【发布时间】:2018-12-29 10:37:34
【问题描述】:
以下函数由 async/await 函数调用,因此我需要从 Mongoose 返回一个真正的 Promise,因此根据 documentation 和 this SO thread 使用“.exec()”。
// where data is an array of documents
function insertNewResults(data) {
return Model.insertMany(data).exec();
}
这样做会给我以下错误:
TypeError: Model.insertMany(...).exec 不是函数 在插入新结果
如果我删除 exec(),我可以毫无问题地插入许多。我使用 exec() 的其他查询似乎没有抛出任何错误,这使得它更加令人困惑。
有人可以解释为什么会这样吗?
编辑 1:下面是我的架构代码
const mongoose = require('mongoose');
const schema = new mongoose.Schema({
date: { type: Date, required: true },
price: { type: Number, required: true },
result: { type: String, required: true }
}, { usePushEach: true });
schema.index(
{ date: -1 }
);
mongoose.model('Model', schema);
【问题讨论】:
-
你能发布你的 schma 代码
-
@AnthonyWinzlet 刚刚通过添加架构代码编辑了我的帖子
标签: node.js database mongodb asynchronous mongoose