AngularJS Mongoose findOne ОК, но ошибка сохранения: DocumentNotFoundError: - PullRequest
0 голосов
/ 27 ноября 2018

В 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 голосов
/ 27 ноября 2018

попробуй сделать так:

 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})...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...