Skip to content

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

Closed

Description

@seven-deuce

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

Activity

  1. added this to the 5.11.15 milestone on Jan 29, 2021
  2. added
    needs repro scriptMaybe a bug, but no repro script. The issue reporter should create a script that demos the issue
    on Jan 29, 2021
  3. IslandRhythms commented on Feb 1, 2021

    @IslandRhythms
    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.

  4. added
    interop issueissue with interop between Mongoose and another npm module that is not a Mongoose prod dependency
    and removed
    needs repro scriptMaybe a bug, but no repro script. The issue reporter should create a script that demos the issue
    on Feb 1, 2021
  5. seven-deuce commented on Feb 2, 2021

    @seven-deuce
    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

  6. IslandRhythms commented on Feb 2, 2021

    @IslandRhythms
    Collaborator

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

  7. seven-deuce commented on Feb 3, 2021

    @seven-deuce
    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

  8. modified the milestones: 5.11.15, 5.11.16 on Feb 3, 2021
  9. IslandRhythms commented on Feb 3, 2021

    @IslandRhythms
    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

Metadata

Metadata

Assignees

Labels

confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.interop issueissue with interop between Mongoose and another npm module that is not a Mongoose prod dependency

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions