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

.lean does not work with mongoose 5.0.0 #6

Closed
ghost opened this issue Jan 24, 2018 · 8 comments
Closed

.lean does not work with mongoose 5.0.0 #6

ghost opened this issue Jan 24, 2018 · 8 comments

Comments

@ghost
Copy link

ghost commented Jan 24, 2018

Hi,

.lean with virtual was working fine when using mongoose 4.0. After updating it to mongoose 5.0.0, we get this error while using .lean with virtuals.

"Error: this.get is not a function",

@vkarpov15
Copy link
Member

Thanks for reporting, opened an issue in the mongoose repo to track

@vkarpov15
Copy link
Member

I can't reproduce this issue, the following script works fine. Can you modify it to reproduce your issue?

const mongoose = require('mongoose');
const mongooseLeanVirtuals = require('./');

const schema = new mongoose.Schema({
  name: String
});
schema.virtual('lowercaseName').get(function() {
  return this.name.toLowerCase();
});
schema.plugin(mongooseLeanVirtuals);

const Model = mongoose.model('t1', schema);

run().catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test');
  await Model.deleteMany({});
  const doc = await Model.create({ name: 'Val' });

  console.log(await Model.findById(doc._id).lean({ virtuals: true }));
  console.log('done');
}

@ghost
Copy link
Author

ghost commented Jan 31, 2018

Hi,

Issue occured if we used alias in schema.

you can reproduce error with this change.

const mongoose = require('mongoose');
const mongooseLeanVirtuals = require('mongoose-lean-virtuals');

const schema = new mongoose.Schema({
  name: { 
    type: String,
    alias: 'Member Name'
  },
});
schema.virtual('lowercaseName').get(function() {
  return this.name.toLowerCase();
});
schema.plugin(mongooseLeanVirtuals);

const Model = mongoose.model('t1', schema);

run().catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test');
  await Model.deleteMany({});
  const doc = await Model.create({ name: 'Val' });

  console.log(await Model.findById(doc._id).lean({ virtuals: true }));
  console.log('done');
}

@vkarpov15
Copy link
Member

Thanks for helping repro. Fix will be in mongoose 5.0.4.

@vmattila
Copy link

Would it be possible to inject this fix also to work in mongoose 4.* branch?

@vkarpov15
Copy link
Member

@vmattila what is preventing you from upgrading to Mongoose 5.x?

@vmattila
Copy link

@vkarpov15 unfortunately the database environment is still on MongoDB 2.6, and it requires 4.x version (https://mongoosejs.com/docs/compatibility.html). That saying, 2.6 is already EOL, so I understand if fixing 4.* is not feasible.

@vkarpov15
Copy link
Member

@vmattila we just shipped 4.13.20 with the fix 👍 Normally we don't backport fixes to 4.x though, so I would strongly recommend upgrading your MongoDB server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants