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

标签 node.js mongodb

美好的一天:

我目前正在从事 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/

相关文章:

node.js - 使用restify时如何支持cors

javascript - 为什么 Puppeteer-firefox 在服务器上不起作用?

node.js - 在heroku上的node.js中追加文件

node.js - 如何通过使用填充或在 mongodb 中使用聚合进行内部查询来使用填充功能

java - MongoDB[Java] : How do I query a stored UUID in byte[ ] format?

node.js - 根据条件加入mongodb集合

node.js - 无法使用heroku将cookie发送到后端

MongoDB - MongoEngine - 保存嵌入式文档不起作用 - 没有属性保存

Java 和 MongoDB - 检查嵌套数组中是否存在字段名称

ruby-on-rails - 如何使用 Grape 和 MongoDB 在 Ruby on Rails 中正确测试 'post'(创建)方法