Angularjs: using forEach to create objects then $ http POST
EDIT:
Node / Inserting express code into sqlite database:
var db = new sqlite3.Database('db/gnarboxmm.db');
router.post('/api/jobs', function(req,res){
tsql = "INSERT INTO Jobs ('Job_File_Name', 'Job_Transporter_ID') VALUES ('"+req.body.File_Name+"','"+req.body.Trans_ID+"')";
console.log(tsql);
db.run(tsql, function(err,rows){
res.end("success");
});
});
UPDATE: I changed my code and is closer now. By adding a $ http request inside the forEach loop I get closer to what I need. New code here:
angular.forEach($rootScope.moveArray, function(value){
$http.post('/api/jobs', {'File_Name':value,
'Trans_ID':'Backup'});
}, $rootScope.jobData);
Still not quite. The server side request object now shows two valid entries (where File_Name and Trans_ID are defined), but has a third entry "undefined".
I am trying to put together a series of filenames that the user interacts with my view, and then implement the filenames into objects that will be posted to the server in $ http.post.
So far I have successfully created an array and parsed the elements of the array into an array of objects ready to go. Here's a piece of code that I used to create an object from my array called "$ rootScope.moveArray"
$scope.sendBackup = function(){
angular.forEach($rootScope.moveArray, function(value){
this.push({'File_Name':value,
'Trans_ID':'Backup'});
}, $rootScope.jobData);
console.log($rootScope.jobData);
The .log console returns "[object, object]" and when I expand I get two objects with the correct names File_Name and Trans_ID.
Here I ran into trouble. I am using $ http.post:
$http.post('/api/jobs', $rootScope.jobData);
Server side validation. I am using express and bodyparser to handle API routing and req / res objects. With code like above, when I register the "File_Name" and "Trans_ID" properties of the req object, I get undefined. I experimented a little. If I give $ rootScope.jobData the index value ($ rootScope.jobData [0]), $ http.post works on the first object in the array. The request object looks like it should with File_Name: '/ validPath' and Trans_ID: 'Backup'
My intuition is that I need to use a different forEach loop for $ rootScope.jobData to get the http.post to work correctly. So far, I have not been successful in this pursuit. Does anyone know a solution to this problem? I doubt angular has no mechanism for arranging object arrays and thinks I'm new to it.
source to share
No one has answered this question yet
See similar questions:
or similar: