3

EDIT:

Node/Express Code inserting 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 am closer now. By adding the $http request inside forEach loop, I'm getting much closer to what I need. New code is here:

angular.forEach($rootScope.moveArray, function(value){
    $http.post('/api/jobs', {'File_Name':value,
                        'Trans_ID':'Backup'});
}, $rootScope.jobData);

Still not quite there. The request object on the server side now shows two valid entries (where File_Name and Trans_ID are defined), but has a third "undefined" entry.

I am attempting to collect a series of file names the user interacting with my view then implement the file names into objects to be sent to the server in $http.post.

So far, I've successfully created an array and parsed out array items into an array of objects that's ready to go. Here's the chunk of code I used to create the 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 console.log returns "[object, object]" and when I drill down I get two objects with properly assigned File_Name's and Trans_ID's.

This is where I run into trouble. I'm using an $http.post:

$http.post('/api/jobs', $rootScope.jobData);

Checking in on the server side. I'm using express & bodyparser to handle API routing and req/res objects. With the code how it is above, when I log the properties "File_Name" and "Trans_ID" of the req object, I get undefined. I have done a bit of experimentation. If I give $rootScope.jobData an index value ($rootScope.jobData[0]), the $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'll need to use another forEach loop for $rootScope.jobData to get $http.post working properly. I haven't been successful in this pursuit thus far. Does anyone know the solution to this issue? I doubt that angular has NO mechanism for posting arrays of objects and think it's the fact that I'm new to it.

  • Open the debugging console in your browser and see what the HTTP activity is. For example in Chrome choose the network tab and it will show what is being sent and the response/errors generated. – whitneyland Sep 16 '14 at 22:48
  • @LeeWhitney the network tab on my Chrome Dev Tools shows a successful post to /api/videos – user3101462 Sep 16 '14 at 22:54
  • Can you post a truncated version of your Node.js code too? My guess is that you're not retrieving the params correctly out of `req.body`. – brandonscript Sep 16 '14 at 23:40
  • Also, fwiw, I'd suggest looking at Q and parallel requests; [here](http://stackoverflow.com/questions/20252640/angular-combining-parallel-and-chained-requests-with-http-then-and-q-all) is a great example. – brandonscript Sep 16 '14 at 23:43
  • OK, what happens if you console.log (or debug) `req.body` in the first line of the route handler? – brandonscript Sep 17 '14 at 00:41

0 Answers0