Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: cyclic dependency detected with Mongo + Nodejs Project

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

like image 300
Dean Avatar asked Dec 19 '22 02:12

Dean


2 Answers

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

    mongoose.connect('mongodb://localhost:27017/gscoo', {autoIndex: false});
like image 168
Blockchain Nerd Avatar answered Dec 20 '22 17:12

Blockchain Nerd


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.

like image 37
Nicolas Guérinet Avatar answered Dec 20 '22 18:12

Nicolas Guérinet