Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

methods defined on the Object/Array prototype throws error when mongoose.populate() is used #9876

Closed
seven-deuce opened this issue Jan 29, 2021 · 5 comments
Assignees
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. interop issue issue with interop between Mongoose and another npm module that is not a Mongoose prod dependency
Milestone

Comments

@seven-deuce
Copy link

Do you want to request a feature or report a bug?
bug
What is the current behavior?
So I have a library in my project that defines a method on the prototype of Array and Object in node.js environment.
All mongoose api works fine, util I call populate() on them, then it throws an error like this:

C:\......\node_modules\mongodb\lib\cmap\connection.js:268
          callback(new MongoError(document));
                   ^

MongoError: BSON field 'OperationSessionInfo.lsid.reach' is an unknown field.
    at MessageStream.messageHandler (C:\....\node_modules\mongodb\lib\cmap\connection.js:268:20)
    at MessageStream.emit (node:events:379:20)
    at processIncomingData (C:\....\node_modules\mongodb\lib\cmap\message_stream.js:144:12)
    at MessageStream._write (C:\.....\node_modules\mongodb\lib\cmap\message_stream.js:42:5)
    at writeOrBuffer (node:internal/streams/writable:400:12)
    at _write (node:internal/streams/writable:341:10)
    at MessageStream.Writable.write (node:internal/streams/writable:345:10)
    at Socket.ondata (node:internal/streams/readable:750:22)
    at Socket.emit (node:events:379:20)
    at addChunk (node:internal/streams/readable:313:12)
    at readableAddChunk (node:internal/streams/readable:288:9)
    at Socket.Readable.push (node:internal/streams/readable:227:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  ok: 0,
  code: 40415,
  codeName: 'Location40415',
  reach: undefined
}

in above example the method defined on prototype is called "reach", thus you see: "OperationSessionInfo.lsid.reach"

If the current behavior is a bug, please provide the steps to reproduce.

Just define a method on the prototype of Object and Array in node, and then try to run a query like this:
SomeCollection.find({id: "some id"}).populate("some_field")

What is the expected behavior?
It should not throw error. Defining a method on the prototypes of Javascript is a valid action and should not conflict with mongoose methods.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: 15.7
mongoose: 5.11.14

@vkarpov15 vkarpov15 added this to the 5.11.15 milestone Jan 29, 2021
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Jan 29, 2021
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Feb 1, 2021
@IslandRhythms
Copy link
Collaborator

'use strict'

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/test', {
  useNewUrlParser: true // Boilerplate for Mongoose 5.x
});

const Person = mongoose.model('Person', mongoose.Schema({
    name: String
  }));
  
  // `ref` tells Mongoose populate what model to query
  const Movie = mongoose.model('Movie', mongoose.Schema({
    title: String,
    director: {
      type: mongoose.ObjectId,
      ref: 'Person'
    },
    actors: [{
      type: mongoose.ObjectId,
      ref: 'Person'
    }]
  }));

  
Array.prototype.test = function(testrun) {};
Object.prototype.reach = function(hello){};


async function test2(){
    const people = await Person.create([
        { name: 'James Cameron' },
        { name: 'Arnold Schwarzenegger' },
        { name: 'Linda Hamilton' }
      ]);
      await Movie.create({
        title: 'Terminator 2',
        director: people[0]._id,
        actors: [people[1]._id, people[2]._id]
      });
let movie = await Movie.find().populate('director');
console.log(movie[0].director.name); // 'James Cameron'
}
test2();

Array doesn't create a problem but Object does.

@IslandRhythms IslandRhythms added interop issue issue with interop between Mongoose and another npm module that is not a Mongoose prod dependency and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Feb 1, 2021
@seven-deuce
Copy link
Author

'use strict'

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/test', {
  useNewUrlParser: true // Boilerplate for Mongoose 5.x
});

const Person = mongoose.model('Person', mongoose.Schema({
    name: String
  }));
  
  // `ref` tells Mongoose populate what model to query
  const Movie = mongoose.model('Movie', mongoose.Schema({
    title: String,
    director: {
      type: mongoose.ObjectId,
      ref: 'Person'
    },
    actors: [{
      type: mongoose.ObjectId,
      ref: 'Person'
    }]
  }));

  
Array.prototype.test = function(testrun) {};
Object.prototype.reach = function(hello){};


async function test2(){
    const people = await Person.create([
        { name: 'James Cameron' },
        { name: 'Arnold Schwarzenegger' },
        { name: 'Linda Hamilton' }
      ]);
      await Movie.create({
        title: 'Terminator 2',
        director: people[0]._id,
        actors: [people[1]._id, people[2]._id]
      });
let movie = await Movie.find().populate('director');
console.log(movie[0].director.name); // 'James Cameron'
}
test2();

Array doesn't create a problem but Object does.

Yes you are right, It is methods defined on the Object prototype,
I had the same bug in Node.js and I reported it and it was fixed in Node version 15.5

@IslandRhythms
Copy link
Collaborator

If they said they fixed it then they reintroduced the same bug in 15.7

@seven-deuce
Copy link
Author

If they said they fixed it then they reintroduced the same bug in 15.7

Nope, it is fixed, I am using 15.7
here is the thread related to that fix in node.js:
nodejs/node#36410

@vkarpov15 vkarpov15 modified the milestones: 5.11.15, 5.11.16 Feb 3, 2021
@IslandRhythms
Copy link
Collaborator

We will be looking into this. Thank you for the report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. interop issue issue with interop between Mongoose and another npm module that is not a Mongoose prod dependency
Projects
None yet
Development

No branches or pull requests

3 participants