gpt4 book ai didi

javascript - 转换为 ObjectId 失败 : Mean Stack

转载 作者:行者123 更新时间:2023-12-03 01:35:50 25 4
gpt4 key购买 nike

在下面的代码中,我尝试更新用户字段。这是我的 editinfo.ts 页面

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;
}
});
}
}

这是我的 auth.service.ts 文件

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());
}

以下代码来自路由文件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);
}
});
});

我在 cmd 中没有收到任何错误,但从 edit info.tsconsole.log(data); 中收到错误为

{message: "Cast to ObjectId failed for value "{ _id: ':id' }" at path "_id" for model "User"", name: "CastError", stringValue: ""{ _id: ':id' }"", kind: "ObjectId", value: {…}, …}

最佳答案

您正在发送字符串“:id”作为用户的 ID。

因此这段代码

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());
}

应该变成这样

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());
}

关于javascript - 转换为 ObjectId 失败 : Mean Stack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51080935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com