【问题标题】:'MongooseError: Callback must be a function, got [object Object]' When using "updateMany" function'MongooseError: Callback must be a function, got [object Object]' 当使用“updateMany”函数时
【发布时间】:2021-06-22 17:13:30
【问题描述】:

忽略乱码。我已经连续工作了好几个小时,还没有时间组织它。

每次 messageCount 为 100 或更多时,我都会收到此错误。

const profileData = await Profiles.findOne({
    userID: message.author.id
});
if (!profileData) return console.log('re')
try {
    if (profileData.messageCount >= 100) {
        console.log('yeet')
        await Profiles.updateMany({
            userID: message.author.id
        }, {
            messageCount: 0,
        }, {
            $inc: {
                caseCount: +1
            }
        }, {
            upsert: true
        });
    } else {
        console.log('na')
    }
} catch (error) {
    console.log(error);
}

【问题讨论】:

  • 我不确定为什么 updateMany() 函数中有 4 个对象而不是 3 个对象。我相信你的意思是把$set:放在{ messageCount: 0, },之前

标签: node.js mongodb


【解决方案1】:

updateMany() 方法只需要 3 个参数,它期望第四个参数是一个可选的回调函数,但是你使用了一个对象,这解释了错误消息。

像这样组合你的 set 和 inc 参数:

await Profiles.updateMany(
  {
    userID: message.author.id
  },
  {
    $set: { messageCount: 0 },
    $inc: { caseCount: 1 },
  },
  {
    upsert: true
  }
);

查看docs了解更多信息。

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    • 2021-11-30
    • 2021-06-25
    • 1970-01-01
    相关资源
    最近更新 更多