Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-907

Cyclic dependency error with aggregate and $geoNear

    XMLWordPrintable

    Details

    • Type: Question
    • Status: Closed
    • Priority: Minor - P4
    • Resolution: Works as Designed
    • Affects Version/s: 2.2.18
    • Fix Version/s: None
    • Component/s: MongoDB 3.2
    • Labels:
      None

      Description

      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)

      Code to reproduce:

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

        Attachments

          Activity

            People

            Assignee:
            Unassigned
            Reporter:
            kbfjesfes David
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: