gpt4 book ai didi

node.js - ensureIndex 请求回调

转载 作者:可可西里 更新时间:2023-11-01 09:23:12 24 4
gpt4 key购买 nike

我需要在 MongoDB 中创建一个索引来存储唯一的 slug。

我使用这段代码生成索引:

this._db = db;
this._collection = this._db.collection("Topics");
this._collection.ensureIndex( { slug: 1 }, { unique: true });

但是当我运行我的测试时,它在“beforeEach”上失败了:(我正在使用 mongo-clean NPM)

beforeEach(function (done) {
clean(dbURI, function (err, created) {
db = created;
instance = topicManager(db);
done(err);
});
});

说:

Uncaught Error: Cannot use a writeConcern without a provided callback

我做错了什么? (如果我评论 ensureIndex 一切正常)

最佳答案

如响应所示,您可能需要为对 ensureIndex 的调用提供回调函数:

this._db = db;
this._collection = this._db.collection("Topics");
this._collection.ensureIndex( { slug: 1 }, { unique: true }, function(error) {
if (error) {
// oops!
}});

关于node.js - ensureIndex 请求回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24476778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com