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

TypeError: Cannot convert object to primitive value #6538

Closed
barhoumio opened this issue May 30, 2018 · 3 comments
Closed

TypeError: Cannot convert object to primitive value #6538

barhoumio opened this issue May 30, 2018 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@barhoumio
Copy link

barhoumio commented May 30, 2018

Do you want to request a feature or report a bug?
Report a bug.

What is the current behavior?
When we override mongoose custom methods like toString() on a schema, then we console.log with a template literals expressions string, we got this error.

 TypeError: Cannot convert object to primitive value
    at Comment.findOne.then.comment (/test.js:37:58)

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

const mongoose = require('mongoose');
const assert = require('assert');
mongoose.connect('mongodb://localhost:27017/test');
const connection = mongoose.connection;

mongoose.plugin(function (schema) {

  schema.methods.toString = function () {
    return `${this.constructor.modelName}(${this._id})`;
  };

});

let commentSchema = new mongoose.Schema({
  title: {
    type: String,
    index: true
  }
});

const Comment = mongoose.model('Comment', commentSchema);

async function run() {
  for (let i in [...Array(10).keys()]) {
    let comment = new Comment({
      title: `comment${i}`,
    });

    await comment.save();
  }

  await Comment
    .findOne({
      title: 'comment5'
    })
    .then(comment => {
        assert.strictEqual(`Comment(${comment._id})`, `${comment}`);
      }
    );

  return connection.close();
};

run().catch(console.error);

What is the expected behavior?
Override mongoose custom methods without using the createWrapper of Kareem lib if the method does not have a hook ( look at /mongoose/lib/services/model/applyHooks.js:67)
Please mention your node.js, mongoose and MongoDB version.
Node v9.11.1
MongoDB v3.2.20
Mongoose v5.1.3

@vkarpov15 vkarpov15 added this to the 5.1.5 milestone Jun 4, 2018
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jun 4, 2018
@vkarpov15
Copy link
Collaborator

Thanks for reporting, will fix ASAP 👍

vkarpov15 added a commit to mongoosejs/kareem that referenced this issue Jun 5, 2018
vkarpov15 added a commit that referenced this issue Jun 5, 2018
@vkarpov15
Copy link
Collaborator

Fixed, fix will be in 5.1.5. Thanks for reporting with a standalone repro script, made it easy to fix 👍

@barhoumio
Copy link
Author

Cool 😃 thank you so much 🍾

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.
Projects
None yet
Development

No branches or pull requests

2 participants