I'm starting out with Angular 2 and I want to do a post call to my node.js server to update data. This is the code in my service:
updateEmployee(id : string, firstName? : string, lastName? : string){
let body : string = JSON.stringify({ "firstName" : "test", "lastName" : "testtwo"});
return this.http.post(this._employeesUrl + id, body)
.map(res => res.json())
.catch(this.handleError);
}
My API route code:
exports.update = function(req, res) {
Employee.model.findById(req.params.id).exec(function(err, item) {
if (err) return res.apiError('database error', err);
if (!item) return res.apiError('not found');
console.log(req);
var data = (req.method == 'POST') ? req.body : req.query;
item.getUpdateHandler(req).process(data, function(err) {
if(req.body.firstName){
item.firstName = req.firstName;
}
if(req.body.lastName){
item.lastName = req.body.lastName;
}
if (err) return res.apiError('create error', err);
res.apiResponse(
200
);
});
});
}
When I do a post call with postman, everything works fine, but in Angular 2 I don't seem to get the body parameter across. How is this done in a right way? I tried many things but none of them worked.
I tried adding the headers in the options but that didn't resolve the problem: