Darth123 Darth123 - 1 month ago 4
TypeScript Question

Ionic 2 Express call not responding to user update function

I'm currently trying to make my Ionic 2 program update a user field when called, in my user-service.ts file I have the following piece of code:

// Update a user
update(user: User): Observable<User> {
let url = `${this.usersUrl}/${user._id}`;
let body = JSON.stringify(user);
let headers = new Headers({'Content-Type': 'application/json'});
console.log(url, body);
return this.http.put(url, body, {headers: headers})
.map(() => user) //See mdn.io/arrowfunctions
.catch(this.handleError);
}


When i call this the server.js file should run the following:

// PUT: update a todo by id
app.put("/api/users/:id", function(req, res) {
console.log('test');
var updateUser = req.body;
delete updateUser._id;

db.collection("users").updateOne({_id: new ObjectID(req.params.id)}, updateUser, function(err, doc) {
if (err) {
handleError(res, err.message, "Failed to update user");
} else {
res.status(204).end();
}
});
});


my console.log in the user-service.ts file when copied and pasted calls the function correctly and displays the test message but this doesn't work by itself. Any suggestions would be much appreciated. Thanks

Answer

The issue seems to be that you need to subscribe something to your update call.

For example:

userService.update(myUser).subscribe((response:any) => {
    //Do something with the response.
});
Comments