1
$scope.submitBracket = function(item, event){
    console.log("submitting form");
    var jsonData = '{"userEmail" : "darwood@mail.com","brackets" :' +     JSON.stringify($scope.allGames) + '}';
    var testJSON = {"Hello": {"id": "file", "value": "WTF"}};
    console.log(jsonData);
    var responsePromise = $http.post("/api/brackets", jsonData, {});
    responsePromise.success(function(dataFromServer,status,headers,config){
    console.log(dataFromServer);
});
responsePromise.error(function(data,status,headers,config){
    console.log(data);
    });

}

I am working on a MEAN stack app which has a form submitting a JSON object to MongoDB. the JSON is valid. It can be submitted to the database manually with no problems. In the above code example I can see the object being output to the console right before being sent to the db via an HTTP post that is the handed off to an express route. Oddly, I had it working and then in testing removed the records from the db to enter new ones and it started submitting a JSON object with an empty array. Sometimes it throws an "empty response message but the POST request does get a 200 response back. I can't for the life of me figure out why the data is not being passed.

If also tried this alternative function on the Angular side to send the data:

$scope.createBracket = function() {
var jsonData = '{"userEmail" : "darwood@mail.com","brackets" :' +     JSON.stringify($scope.allGames) + '}';
    $http.post('/api/brackets', jsonData)
        .success(function(data) {
        $scope.brackets = data;
        $location.path('/');
    })
    .error(function(data) {
        console.log('Error: ' + data);
    });

};

This is the express route that it is passed off to.

app.post('/api/brackets', function(req, res) {

    // create a Bracket
    Bracket.create({
        text : req.body.text,
        done : false
    }, function(err, Bracket) {
        if (err)
            res.send(err);

    });

});

when using Postman to test with raw JSON. It hangs for a long time and says that the request is pending. This code was modeled after the tutorial found here

http://scotch.io/tutorials/javascript/creating-a-single-page-todo-app-with-node-and-angular

I have this sample code up and running and inserting into my database with no problem.

Stumped I am. Any help would be appreciated. Thanks.

Stennie
  • 63,885
  • 14
  • 149
  • 175
djpATLSF
  • 243
  • 1
  • 3
  • 5

2 Answers2

0

I had a similar problem using Grails as the backend, so this might not help you. I had to explicitly set the Content-Type.

$http({
    method: 'POST',
    url: serverURL + '/' + scope.controller + '/save',
    data: $form.serializeObject(),
    headers: {'Content-Type': 'application/json'}
})

I would have simply commented, but I don't have the reputation to allow that.

Ken
  • 685
  • 2
  • 5
  • 11
  • Yeah, `Postman` doesn't send Content-Type for some reason. I've seen people encounter this before. – mscdex May 29 '14 at 12:44
0

How did you send the JSON object to MongoDB manually? Was it using command line or with tool like Robomongo?

I would try the following. No 1. WHAT EXACTLY ARE WE SENDING TO SERVER?

$scope.createBracket = function() {
var jsonData = '{"userEmail" : "darwood@mail.com","brackets" :' +      
       JSON.stringify($scope.allGames) + '}';

console.info("In Controller-> jsonData = " + JSON.stringify(jsonData)); //**TEST**
//Is jsonData shown as expected?    

$http.post('/api/brackets', jsonData)

    .success(function(data, status, headers, config) {
    $scope.brackets = data;
    console.info("Status: " + status);
    console.info("Data: " + data);
    console.info("Config: " + JSON.stringify(config));
    //$location.path('/');                        
  })

  .error(function(data) {
    console.log('Error: ' + data);
    });

No 2. ARE WE GETTING POST REQ DATA AT SERVER?

app.post('/api/brackets', function(req, res) {
console.info("Body: " + JSON.stringify(req.body));        //TEST
console.info("Text: " + JSON.stringify(req.body.text));   //TEST
console.info("Done: " + JSON.stringify(req.body.done));   //TEST    
//Have we got the data from the client?
//If so need to check Mongoose Schema/Model MongoDB settings etc

//create a Bracket
//I take it you are using Mongoose and Bracket is the name of the Mongoose Model?
//
Bracket.create({
    text : req.body.text,
    done : false
}, function(err, bkt)  //I would remove Bracket from fn argument as its your Mongoose M
                       //model name
{
    if (err)
        res.send(err);
});
});
Mick Cullen
  • 9,214
  • 3
  • 21
  • 17
  • I tried both steps. Step 2: the req.body is populated with the expected perfectly formed JSON doc to go into MongoDB, req.body.text is undefined, req.body.done is undefined as well. I tried inserting just req.body and that did not work. I wasn't sure if that would work. I tried adding the content-type headers as Ken suggested and that did not work either. In the meantime I am continuing to research causes of the req.body.text being undefined. – djpATLSF May 29 '14 at 18:35
  • Does not look like the parameters done and text exist in you program. In the example from scottch.io data from the form formdata.text was posted and done did not need to be posted as for a new todo done = false always. Thus done was assigned server side. If you are not using a form you could do $scope.appdata.text="";$scope.appdata.userEmail="" etc... and then $http.post('/api/brackets' $scope.appdata). Then your server will be sent a req.body.text etc. Ensure your mongoose Schema matches all the parameters. – Mick Cullen May 29 '14 at 19:32
  • Mike, that is exactly what it was. What a stupid mistake. I got it working now. Thanks for your help. – djpATLSF May 29 '14 at 22:48