Error: cyclic dependency detected with Mongo + Nodejs Project

Viewed 13423

Good day:

I'm currently working on a NodeJs + MongoDB project. My solution is simple, I have 3 collections (Client, Scope, Grant). Scope forms a many-to-many relationship between Client & Grant collections. Currently I'm querying a Client record, then getting all the Scope collection assigned to that Client then finally, getting all the Grant records in the Scope collection I previously queried. This is my code:

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);
         })
     });


 });
}

When I run my code, I'm getting this:

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 }

One thing that's interesting, I found that this is the code causing the issue however, I'm lost as to why console.log(grants.toArray());

4 Answers

In the mongoose connect, set autoIndex to false like this:

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

I think the issue is related to the type of clientId and clientSecret. For example, they can be objects while there are expected to be variables. If they are objects they should be converted into variables. Otherwise, it would be important to check if there aren't any characters in clientId and clientSecret that generate a parsing problem.

You can remove the options at the end of the MongoDB connection string.

I accidentally happened to insert a collection into a $set operation, which then caused that error.

Since there is no useful stack trace in the error I had to debug a bit around until I found the operation that failed.

So maybe your $set operation is malformed, for example due to a broken function call that has its parameters swapped.

Before I found the real cause, I looked into upgrading bson instead. But an up to date bson package is only included in mongodb 4.0, which is in beta right now.

Related