node.js - Mongoose insertMany()返回回调不是传递选项时的函数错误
问题描述
我正在使用 mongoose版本 4.7.2并且需要将大量数据插入到集合中,而不会出现重复和验证错误作为承诺。
代码 :
module.exports.addCustomers = function (customers) {
return Customer.insertMany(customers,{ordered :false})
}
尝试使用 insertMany() 插入文档时发生以下错误。
TypeError:回调不是函数
当我从我的代码中删除 {ordered :false} 时没有错误。
解决方案
这有点奇怪。如果您将响应作为承诺处理,它应该能够在没有回调的情况下工作。您可以尝试在函数内执行查询:
module.exports.addCustomers = async function (customers) {
const result = await Customer.insertMany(customers, {ordered: false})
return result
}
我已经在 mongoose 上测试过了5.2.12
。
推荐阅读
- vhdl - 可合成的 VHDL 递归,Vivado:模拟器以意外方式终止
- c# - 将 REST API 转换为 websocket 客户端
- android-studio - Android studio 3.6 canary 6 不启动只是加载
- angular - 条形图中未显示低值的条形
- google-chrome - GMail 阻止通过自动化(Selenium)登录
- html - 如何从 html 文件中 grep 特定日期
- angular - 在 Angular 7 表单控件的单个输入字段中验证电子邮件、电话和 PAN 号码
- javascript - 如何将数组映射到对象
- javascript - 使用 RSACryptoServiceProvider 解密 crypto.subtle 加密文本中的问题
- java - org.springframework.core.io.UrlResource 无法从几个网站读取内容