转换为ObjectId失败:平均堆栈

时间:2018-06-28 10:46:25

标签: javascript node.js angular express mean-stack

在下面的代码中,我尝试更新用户字段。这是我的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中没有收到任何错误,但是来自console.log(data);的{​​{1}}中的错误是

  

{消息:路径上的值“ {_id:':id'}的对象ID转换失败”   模型“ User”的“ _id””,名称:“ CastError”,stringValue:“” {_id:   ':id'}“”,种类:“ ObjectId”,值:{…},...}

1 个答案:

答案 0 :(得分:1)

您正在发送字符串':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());
  }