AngularJS猫鼬findOne可以,但是保存错误:DocumentNotFoundError:

时间:2018-11-27 16:17:58

标签: angularjs mongodb mongoose

在下面的client.save()上,出现以下错误(可以正确捕获):

DocumentNotFoundError:未找到查询“ {_id:'5bfbce595be7d1047c976e6b'}”的文档

app.put('/api/client', function (req, res) {
    Client.findOne(new mongoose.Types.ObjectId(req.body._id)).then(client => {
            //This is OK, I can see client and its properties
            client.name = req.body.name;
            //This is OK, I can see the updated client and its properties
            client.save().then(test => {
                console.log("ERR=" + err);
                console.log(test);
            }).catch(err => console.log("ERR :" + err));
            res.json(client);
        });
});

模型是这样的:

 mongoose.model('Client', {
    _id: {type: String, default: ''},
    name: {type: String, default: ''},
    creationDate: {type: Date, default: ''}
});

为什么在FindOne()上找到文档,而在save()上找不到文档?

1 个答案:

答案 0 :(得分:0)

尝试这样做:

 mongoose.model('Client', {
  _id: { type: mongoose.Schema.Types.ObjectId, auto: true },
  name: { type: String, default: '' },
  creationDate: { type: Date, default: Date.now() }
});

还有

Client.findOne().where({_id : req.body._id})...