mongoosejs / mongoose-lean-virtuals

Attach virtuals to the results of mongoose lean() queries
Apache License 2.0
45 stars 24 forks source link

Using mongoose model with cursor returns undefined #10

Closed kikill95 closed 5 years ago

kikill95 commented 6 years ago

Hello, and thank you for a great plugin

I've encountered an issue, when I am using cursor

Few code of example (migrations, so I do not care about performance)

  let db = await require('../../util/db').connect()
  console.log('Started ...')
  try {
    const cursor = DocModel.find().cursor()
    for (let doc = await cursor.next(); doc; doc = await cursor.next()) {
      console.log('Processing document...', doc._id)
      try {
        await doc.save() // trigger hooks
      } catch (err) {
        console.log('Could not execute:', err)
      }
    }
  } catch (error) {
    console.error(error)
  }
  console.log('Finished ...')
  await db.disconnect()

And in model:

const DocModelSchema = new mongoose.Schema({
  // ...
})
DocModelSchema.plugin(mongoosePaginate)
module.exports = mongoose.model('DocModel', DocModelSchema)

When I am commenting DocModelSchema.plugin(mongoosePaginate) - everything works as expected

kikill95 commented 6 years ago

Prepared PR for this, @vkarpov15 I am not sure that this is how this should be, could you please take a look?

vkarpov15 commented 6 years ago

@kikill95 thanks for identifying this. I'll deploy your changes to mongoose-lean-virtuals later

kikill95 commented 5 years ago

This was merged