Reputation:
In the below code, I trying to update the user fields. This is my editinfo.ts
page
userSpecificData:any = {_id:'-',name: '-', email: '-', username: '-', password: '-', phone: '-',education:'-',location:'-',title:'-',company:'-'}
onEditFormSubmit(){
const user = {
_id:this.userSpecificData._id,
name: this.userSpecificData.name,
email: this.userSpecificData.email,
username: this.userSpecificData.username,
password: this.userSpecificData.password,
phone:this.userSpecificData.phone,
location:this.userSpecificData.location,
title:this.userSpecificData.title,
company:this.userSpecificData.company,
education:this.userSpecificData.education
}
if (this.EditPageUserForm.dirty && this.EditPageUserForm.valid) {
this.authService.updateUserData(user).subscribe(data => {
console.log(data);
if(data.success){
this.flashMessage.show('You are now successfully registered, redirecting to Login page..', {cssClass: 'alert-success', timeout: 4000});
//setTimeout(function(){
this.navCtrl.push(LoginPage);
// },4000)
console.log('sucess');
} else {
this.flashMessage.show('Something went wrong', {cssClass: 'alert-danger', timeout: 4000});
// this.router.navigate(['/register']);
console.log('faild');
return false;
}
});
}
}
This is my auth.service.ts
file
updateUserData(user) {
let headers = new Headers();
console.log(headers,user);
headers.append('Content-Type','application/json');
return this.http.put('http://localhost:3000/users/user/:id', user,{headers: headers})
.map(res => res.json());
}
The below code is from the routes file users.js
router.put('/user/:id', function(req, res){
User.findByIdAndUpdate({_id: req.params.id},
{
name: req.body.name,
email: req.body.email,
username: req.body.username,
phone:req.body.phone,
location:req.body.location,
title:req.body.title,
company:req.body.company,
education:req.body.education
}, function(err, docs){
if(err) res.json(err);
else
{
console.log(docs);
res.redirect('/user/'+req.params.id);
}
});
});
I'm not getting any error in my cmd but from in console.log(data);
from edit info.ts
I'm getting an error as
{message: "Cast to ObjectId failed for value "{ _id: ':id' }" at path "_id" for model "User"", name: "CastError", stringValue: ""{ _id: ':id' }"", kind: "ObjectId", value: {…}, …}
Upvotes: 0
Views: 110
Reputation: 1847
You are sending the string ':id' as the id of the user.
Thus this code
updateUserData(user) {
let headers = new Headers();
console.log(headers,user);
headers.append('Content-Type','application/json');
return this.http.put('http://localhost:3000/users/user/:id', user,{headers: headers})
.map(res => res.json());
}
should become this
updateUserData(user) {
let headers = new Headers();
console.log(headers,user);
headers.append('Content-Type','application/json');
return this.http.put('http://localhost:3000/users/user/' + user._id, user,{headers: headers})
.map(res => res.json());
}
Upvotes: 1