Mongoose函数findByIdAndUpdate返回一个空文档

时间:2017-01-02 00:59:29

标签: mongoose

我正在运行Mongoose findByIdAndUpdate函数(在Model Static函数内):

TemplateSchema.statics.archive = function(templateId, userId, callback) {

    let now = new Date();

    this.model('Template').findByIdAndUpdate(templateId, {
            discon: true,
            discon_date: now,
            discon_user_id: userId
        }, {new: true}, function(err, templateDoc){
            if(err){
                console.log("err:",err);
            }
            else{
                console.log("templateDoc:",templateDoc);
            }
        });
};

控制台打印出templateDoc: null。知道为什么吗?

备注: 在模板架构中,_id字段指定为:

_id: {type: String}

当我执行console.log(typeof templateId)时,在运行findByIdAndUpdate功能之前,它显示为string

我使用的是Mongoose v4.7.6。

我已经检查过Mongo数据库中是否存在该文档。

我已检查过Mongo中_id字段的类型,它是String

1 个答案:

答案 0 :(得分:0)

解决这个问题的唯一方法就是这样做(尽管如果templateIdstring的类型,我不知道为什么需要这样做):

templateId = JSON.parse(templateId);
相关问题