【问题标题】:Mongoose save await throws error called level errorMongoose save await 抛出称为级别错误的错误
【发布时间】:2019-07-31 20:36:24
【问题描述】:
async add(data) {
    return new Promise(async (resolve, reject) => {

        let model = new Model();
        model.name = data.name;

        try {
            await mainCategory.save();

            resolve(true);
        } catch (err) {   
            reject(err);
        }
    });
}

我已经编写了一个类似 thise 的代码来使用 mongoose 将数据保存到 mongodb,它会抛出这样的错误

{
   "level": "error"
}

这个错误是从捕获中给出的。

这是我的模型架构

let category = new Schema({
  name: {
    type: String,
    unique: true,
    required: true
  },
  imageUrl: {
    imageUrl
  },
  subCategories: [{
    type: Schema.Types.ObjectId,
    ref: 'SubCategory'
  }]
});

【问题讨论】:

  • "level": "error" 非常适合您的模型。这个问题缺少 stackoverflow.com/help/mcve ,只有你可能知道它为什么在那里。你试过检查err吗?它应该不仅仅是普通的对象。此外,您正在做的事情是承诺构建反模式。当你有一个承诺时,永远不要使用new Promise。永远不要使用async 作为Promise 回调。
  • @estus 我已经添加了我的架构
  • 架构无法解释问题所在。您没有澄清有关该错误的任何信息。你是怎么检查的。附加调试器的屏幕截图会有所帮助。这不是已知的 Mongoose 错误。此时你的问题是无解的。请提供允许其他用户解决问题的 MCVE - 一个 repo,任何东西。

标签: javascript node.js mongoose ecmascript-6 async-await


【解决方案1】:
async add(data){
    model.name = data.name;
    let  model = new Model();
    model.save()
    .then(callback => {
        console.log(callback)
    })
    .catch(err => console.error(err))
}

Try this

【讨论】:

  • 您能否添加更多详细信息,说明为什么这会解决问题?
  • 你试过了吗..你的架构设计不行..重新设计你的架构
  • ```` let category = new Schema({ name: { type: String, unique: true, required: true }, imageUrl: { type: String }, subCategories: { type: Schema. Types.ObjectId, ref: 'SubCategory' } });
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
  • 2021-04-09
  • 2021-09-19
  • 2017-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多