gpt4 book ai didi

node.js - 错误 : cyclic dependency detected with Mongo + Nodejs Project

转载 作者:可可西里 更新时间:2023-11-01 09:21:19 34 4
gpt4 key购买 nike

美好的一天:

我目前正在从事 NodeJs + MongoDB 项目。我的解决方案很简单,我有 3 个集合(Client、Scope、Grant)。 Scope 在 Client 和 Grant 集合之间形成多对多关系。目前我正在查询一个 Client 记录,然后获取分配给该 Client 的所有 Scope 集合,最后获取所有 Grant 我之前查询过的 Scope 集合中的记录。这是我的代码:

getClient (clientId, clientSecret, callback) {
let that = this;
this.mongoClient.collection('client').findOne({"client" : clientId, "client_secret" : clientSecret}, function (err, client) {
if (err) {
return callback(err, null);
}
that.mongoClient.collection('scope').find({'client_id': client._id}, {"service_id":1}, function(err, serviceIds) {
that.mongoClient.collection('grant').find({'_id': { $exists : true, $in : serviceIds }}, function(err, grants) {
console.log(grants.toArray());
callback(err, client);
})
});


});
}

当我运行我的代码时,我得到了这个:

Promise {


<rejected> Error: cyclic dependency detected
at serializeObject (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:296:33)
at serializeInto (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at serializeObject (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:308:18)
at serializeInto (/home/vagrant/api/node_modules/bson/lib/bson/parser/serializer.js:776:17)
at BSON.serialize (/home/vagrant/api/node_modules/bson/lib/bson/bson.js:58:27)
at Query.toBin (/home/vagrant/api/node_modules/mongodb-core/lib/connection/commands.js:140:25)
at Pool.write (/home/vagrant/api/node_modules/mongodb-core/lib/connection/pool.js:986:23)
at Cursor._find (/home/vagrant/api/node_modules/mongodb-core/lib/cursor.js:286:22)
at nextFunction (/home/vagrant/api/node_modules/mongodb-core/lib/cursor.js:591:10)
at Cursor.next [as _next] (/home/vagrant/api/node_modules/mongodb-core/lib/cursor.js:699:3)
at fetchDocs (/home/vagrant/api/node_modules/mongodb/lib/cursor.js:857:10)
at toArray (/home/vagrant/api/node_modules/mongodb/lib/cursor.js:884:3)
at /home/vagrant/api/node_modules/mongodb/lib/cursor.js:841:5
at Promise (<anonymous>)
at Cursor.toArray (/home/vagrant/api/node_modules/mongodb/lib/cursor.js:840:10)
at /home/vagrant/api/services/TokenService.js:20:25 }

一件有趣的事,我发现这是导致问题的代码,但是,我不知道为什么 console.log(grants.toArray());

最佳答案

在 mongoose 连接中,像这样将 autoIndex 设置为 false:

    mongoose.connect('mongodb://localhost:27017/gscoo', {autoIndex: false});

关于node.js - 错误 : cyclic dependency detected with Mongo + Nodejs Project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47259901/

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