【问题标题】:save method not working in Mongoose保存方法在猫鼬中不起作用
【发布时间】:2018-04-10 18:52:03
【问题描述】:

我在 node.js 中使用以下代码:

async function create(data) {
    utils.removeIdFields(data);
    const user = new User(data);
    console.log("user will be printed here", user);
    await user.save();
    console.log("code not reaching here");
    return user;
}

在上面的代码中,User 是一个猫鼬模型,在 user.save() 上面的行之前它工作正常。但是,它从不打印下一条日志。

我使用的是 mongoose 版本 4.11.0。

【问题讨论】:

  • 你能给我看看型号吗?
  • 确保将行添加到模型中:mongoose.Promise = Promise;
  • 是的,这个试过了,还是不行..@HaroonKhan
  • @KshitijMittal 一样!你找到解决方案了吗?

标签: node.js mongodb mongoose async-await


【解决方案1】:

我使用以下代码连接到 MongoDB:

function connectMongoDb() {
    if (mongoose.connect(makeMongoUrl(), { useMongoClient: true })) {
        mongoose.plugin(plugins.transformer);
    }
}

事实证明,删除 { useMongoClient: true } 并仅使用以下代码解决了问题:

function connectMongoDb() {
    if (mongoose.connect(makeMongoUrl())) {
        mongoose.plugin(plugins.transformer);
    }
}

【讨论】:

    猜你喜欢
    • 2015-11-16
    • 1970-01-01
    • 2021-03-10
    • 2016-11-04
    • 1970-01-01
    • 2016-05-04
    • 2017-03-08
    • 1970-01-01
    • 2013-03-14
    相关资源
    最近更新 更多