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?
console.log("New book: " + JSON.stringify(req.body, null, 4));to see exactly what the object contains.success()and.error()are deprecated. It is preferred to use the returned promise with.then(successCB, failureCB)