首页 > 解决方案 > Mongoose insertMany()返回回调不是传递选项时的函数错误

问题描述

我正在使用 mongoose版本 4.7.2并且需要将大量数据插入到集合中,而不会出现重复和验证错误作为承诺

代码 :

  module.exports.addCustomers = function (customers) {
   return Customer.insertMany(customers,{ordered :false})
   }

尝试使用 insertMany() 插入文档时发生以下错误。

TypeError:回调不是函数

控制台错误

当我从我的代码中删除 {ordered :false} 时没有错误。

标签: node.jsmongodbmongoose

解决方案


这有点奇怪。如果您将响应作为承诺处理,它应该能够在没有回调的情况下工作。您可以尝试在函数内执行查询:

module.exports.addCustomers = async function (customers) {
    const result = await Customer.insertMany(customers, {ordered: false})
    return result
}

我已经在 mongoose 上测试过了5.2.12


推荐阅读