美好的一天:
我目前正在研究NodeJs + MongoDB项目。我的解决方案很简单,我有3个集合(Client,Scope,Grant)。范围形成了客户与客户之间的多对多关系。授予收藏。目前我正在查询客户端记录,然后将所有 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());
答案 0 :(得分:6)
在mongoose connect中,将autoIndex设置为false,如下所示:
mongoose.connect('mongodb://localhost:27017/gscoo', {autoIndex: false});
答案 1 :(得分:1)
我认为问题与clientId和clientSecret的类型有关。例如,它们可以是对象,而预期会有变量。如果它们是对象,则应将其转换为变量。 否则,重要的是检查clientId和clientSecret中是否没有任何产生解析问题的字符。
答案 2 :(得分:1)
您可以删除MongoDB连接字符串末尾的选项。
答案 3 :(得分:0)
我不小心在 $set 操作中插入了一个集合,这导致了那个错误。
由于错误中没有有用的堆栈跟踪,我不得不稍微调试一下,直到发现操作失败。
所以也许您的 $set 操作格式不正确,例如由于交换了参数的损坏的函数调用。
在我找到真正的原因之前,我转而考虑升级 bson。但是最新的 bson 包仅包含在 mongodb 4.0 中,目前处于测试阶段。