1

I'm starting out learning the MEAN stack, so apologies of this is a stupid question. I'm adapting a few tutorials to my own project, and I've made a simple REST api to a database of books.

// add book and return updated list
app.post('/api/books', function(req, res) {
    console.log("New book: ", JSON.stringify(req.body, null, 4));
    Book.create({
        title: req.body.title,
        author: req.body.author,
        pageCount: 100,
    }, function(err, book) {
        if (err)
            res.send(err);

        // get and return updated list
        Book.find(function(err, books) {
            if (err)
                res.send(err);
            res.json(books);
        });
    });
});

I'm trying to use angular to interface with this, like so:

$scope.createBook = function() {
    console.log('Trying to create ', $scope.formData);
    $http({ method: 'POST', url: '/api/books', data: $scope.formData}).then(function (response) {
        $scope.formData = {};   // reset form
        console.log('CREATE Success: ', response);
    }, function (error) {
        console.log('CREATE Error: ', error);
    });
};

The output I get from the createBook-function is Trying to create – {title: "amazingTitle", author: "amazingAuthor"}, which is as it should be, but the output from the API handler is New book: {}, which is of course not what I want. I realise this might not be enough detail to go on, but where could I be going wrong here?

12
  • It might help you to do console.log("New book: " + JSON.stringify(req.body, null, 4)); to see exactly what the object contains Commented Jun 25, 2016 at 15:14
  • Also, and this is a small detail, the .success() and .error() are deprecated. It is preferred to use the returned promise with .then(successCB, failureCB) Commented Jun 25, 2016 at 15:17
  • @pulseOne Thanks, I tried that, and I got New book: {} Commented Jun 25, 2016 at 15:17
  • 1
    check in browser debug tools whether they are getting posted to server & also make sure you are using body parser module on the server side Commented Jun 25, 2016 at 15:19
  • In your node server, are you using a body parser? Commented Jun 25, 2016 at 15:19

2 Answers 2

1

It turned out to be an error with the body-parser setup, I'd only set the app to use urlencoded, not json.

Sign up to request clarification or add additional context in comments.

Comments

-2

JSON.stringify() your data.

c.f. AngularJS - $http.post send data as json

my POST calls typically take this form:

$http({method: 'POST', url: url, headers: headers, data: JSON.stringify(request)}).then(function(response) {
        var data = response.data;
        // do something
    }, function(error) {
        // do something
    });

2 Comments

No need to stringify...it is done by default internally in $http api
Nothing wrong with OP signature for $http.post('/api/books', $scope.formData). i personally use that a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.