Cyclic dependency error with aggregate and $geoNear #4866

Closed
root-io opened this Issue Jan 3, 2017 · 3 comments

Projects

None yet

3 participants

@root-io
root-io commented Jan 3, 2017 edited
const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
const { Schema } = mongoose;

const connect = () => {
  const options = {
    promiseLibrary: require('bluebird')
  };
  mongoose.connect('mongodb://localhost:27017/mongoose_test', options);
};
connect();

mongoose.connection.on('error', (err) => {
  console.error(err);
});

const UserSchema = new Schema({
  location: {
    type: { type: String, default: 'Point' },
    coordinates: { type: Array, default: [0, 0] }
  }
});
UserSchema.index({ location: '2dsphere' });

const User = mongoose.model('User', UserSchema);

const userId = '586d3620f6da431b4aec1a12';

let user = new User({
  _id: userId
})

user.save();

function getUserLocation() {
  return User.findById(userId)
  .then((user) => {
    return new Promise((resolve, reject) => {
      if (user && user.location) {
        resolve(user.location);
      } else {
        reject();
      }
    });
  });
}

function myAwesomeFunction(userLocation) {
  return User.aggregate([{
    $geoNear: {
      near: userLocation,
      distanceField: 'distance',
      maxDistance: 1000,
      spherical: true
    }
  }
 ]);
}

getUserLocation()
.then(myAwesomeFunction)
.then(console.log)
.catch(console.error);
Error: cyclic dependency detected
    at serializeObject (/project/node_modules/bson/lib/bson/parser/serializer.js:296:33)
    at serializeInto (/project/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/project/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/project/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/project/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/project/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/project/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/project/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/project/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/project/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/project/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/project/node_modules/bson/lib/bson/parser/serializer.js:618:17)
    at serializeObject (/project/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/project/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at BSON.serialize (/project/node_modules/bson/lib/bson/bson.js:58:27)
    at Query.toBin (/project/node_modules/mongodb-core/lib/connection/commands.js:140:25)
    at Pool.write (/project/node_modules/mongodb-core/lib/connection/pool.js:921:23)
    at Server.command (/project/node_modules/mongodb-core/lib/topologies/server.js:515:15)
    at Server.command (/project/node_modules/mongodb/lib/server.js:376:17)
    at executeCommand (/project/node_modules/mongodb/lib/db.js:310:19)
    at Db.command (/project/node_modules/mongodb/lib/db.js:334:44)
    at Collection.aggregate (/project/node_modules/mongodb/lib/collection.js:2686:13)
    at NativeCollection.(anonymous function) [as aggregate] (/project/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:126:28)
    at /project/node_modules/mongoose/lib/aggregate.js:576:10
    at Aggregate.exec (/project/node_modules/mongoose/lib/aggregate.js:562:10)
    at Aggregate.then (/project/node_modules/mongoose/lib/aggregate.js:606:15)

Node.js 6.9.2
MongoDB 3.2.11
Mongoose 4.7.6

@varunjayaraman
Collaborator

Could you please post your entire Schema and maybe some example documents so I can easily attempt to reproduce this? I don't know what myLocation is either.

@root-io
root-io commented Jan 5, 2017 edited

Updated 😉, you need to execute the example a second time to see the error

Error: cyclic dependency detected
    at serializeObject (/project/test/node_modules/bson/lib/bson/parser/serializer.js:296:33)
    at serializeInto (/project/test/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/project/test/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/project/test/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/project/test/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/project/test/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/project/test/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/project/test/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/project/test/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/project/test/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at serializeObject (/project/test/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/project/test/node_modules/bson/lib/bson/parser/serializer.js:618:17)
    at serializeObject (/project/test/node_modules/bson/lib/bson/parser/serializer.js:308:18)
    at serializeInto (/project/test/node_modules/bson/lib/bson/parser/serializer.js:776:17)
    at BSON.serialize (/project/test/node_modules/bson/lib/bson/bson.js:58:27)
    at Query.toBin (/project/test/node_modules/mongodb-core/lib/connection/commands.js:140:25)
    at Pool.write (/project/test/node_modules/mongodb-core/lib/connection/pool.js:921:23)
    at Server.command (/project/test/node_modules/mongodb-core/lib/topologies/server.js:515:15)
    at Server.command (/project/test/node_modules/mongodb/lib/server.js:376:17)
    at executeCommand (/project/test/node_modules/mongodb/lib/db.js:310:19)
    at Db.command (/project/test/node_modules/mongodb/lib/db.js:334:44)
    at Collection.aggregate (/project/test/node_modules/mongodb/lib/collection.js:2686:13)
@varunjayaraman varunjayaraman added this to the 4.8 milestone Jan 6, 2017
@vkarpov15 vkarpov15 modified the milestone: 4.7.7, 4.8 Jan 6, 2017
@varunjayaraman varunjayaraman modified the milestone: 4.10, 4.7.7 Jan 6, 2017
@varunjayaraman
Collaborator

Ok I confirmed that I get the cyclic dependency error. Here's a clearer repro script, using most of the same logic that @root-io used:

const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
const { Schema } = mongoose;

const GITHUB_ISSUE = `gh-4866`;
const connect = () => {
  mongoose.connect(`mongodb://localhost:27017/${GITHUB_ISSUE}`);

  mongoose.connection.on('error', (err) => {
    console.error('Error', err);
  });
};

connect();



const UserSchema = new Schema({
  location: {
    type: { $type: String, default: 'Point' },
    coordinates: { $type: Array, default: [0, 0] }
  }
}, { typeKey: '$type' });
UserSchema.index({ location: '2dsphere' });

const User = mongoose.model('User', UserSchema);

const userId = '586d3620f6da431b4aec1a12';

let user = new User({
  _id: userId
});

function getUserLocation() {
  return User.findById(userId)
    .then((user) => {
      return new Promise((resolve, reject) => {
        if (user && user.location) {
          resolve(user.location);
        } else {
          reject();
        }
      });
    });
}

function myAwesomeFunction(userLocation) {
  return User.aggregate([{
    $geoNear: {
      near: userLocation,
      distanceField: 'distance',
      maxDistance: 1000,
      spherical: true
    }
  }
  ]);
}

user.save()
  .then(getUserLocation)
  .then(myAwesomeFunction)
  .then(docs => console.log('docs', docs))
  .catch(error => console.error('error', error));

This produces the following error:

Error: cyclic dependency detected
    at serializeObject (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bso
n/parser/serializer.js:296:33)
    at serializeInto (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bson/
parser/serializer.js:776:17)
    at serializeObject (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bso
n/parser/serializer.js:308:18)
    at serializeInto (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bson/
parser/serializer.js:776:17)
    at serializeObject (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bso
n/parser/serializer.js:308:18)
    at serializeInto (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bson/
parser/serializer.js:776:17)
    at serializeObject (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bso
n/parser/serializer.js:308:18)
    at serializeInto (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bson/
parser/serializer.js:776:17)
    at serializeObject (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bso
n/parser/serializer.js:308:18)
    at serializeInto (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bson/
parser/serializer.js:776:17)
    at serializeObject (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bso
n/parser/serializer.js:308:18)
    at serializeInto (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bson/
parser/serializer.js:618:17)
    at serializeObject (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bso
n/parser/serializer.js:308:18)
    at serializeInto (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bson/
parser/serializer.js:776:17)
    at BSON.serialize (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/bson/lib/bson
/bson.js:58:27)
    at Query.toBin (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/mongodb-core/lib
/connection/commands.js:140:25)
    at Pool.write (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/mongodb-core/lib/
connection/pool.js:921:23)
    at Server.command (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/mongodb-core/
lib/topologies/server.js:515:15)
    at Server.command (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/mongodb/lib/s
erver.js:376:17)
    at executeCommand (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/mongodb/lib/d
b.js:313:19)
    at Db.command (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/mongodb/lib/db.js
:339:44)
    at Collection.aggregate (/Users/roonie/workspace/oss/mongoose/gh-4866/node_modules/mongodb
/lib/collection.js:2686:13)

The same query that fails in mongoose seems to work in mongo:

db.users.aggregate([    {      $geoNear: {         near: { type: "Point", coordinates: [ 0 , 0 ] },         distanceField: "distance",         spherical: true      }    } ])

From the shell, that returns:

{ "_id" : ObjectId("586d3620f6da431b4aec1a12"), "location" : { "coordinates" : [ 0, 0 ], "type" : "Point" }, "__v" : 0, "distance" : 0 }
@vkarpov15 vkarpov15 added a commit that referenced this issue Jan 12, 2017
@vkarpov15 vkarpov15 test: repro #4866 ab680e4
@vkarpov15 vkarpov15 closed this in dd8003b Jan 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment