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

isValid virtual generates a crash when updating a nested property #12124

Closed
2 tasks done
BobBatard opened this issue Jul 19, 2022 · 0 comments
Closed
2 tasks done

isValid virtual generates a crash when updating a nested property #12124

BobBatard opened this issue Jul 19, 2022 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@BobBatard
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.4.4

Node.js version

16.15.0

MongoDB server version

5.0.0

Description

Adding a virtual named isValid to a schema which have a nested property and updating the property while setting a value to the virtual generates a crash on save.

Steps to Reproduce

Here's a reproduction script

import mongoose from 'mongoose'
import assert from 'node:assert'


(async () => {
  await mongoose.connect('mongodb://localhost:27017/test', {
    useNewUrlParser: true,
    useUnifiedTopology: true
  })

  await mongoose.connection.dropDatabase()

  const Schema = new mongoose.Schema({
    test: String,
    data: { sub: String }
  })

  Schema.virtual('isValid')

  const testSchema = mongoose.model('Test', Schema)

  await testSchema.create({})
  const doc = await testSchema.findOne()

  doc.set('isValid', true)

  // working
  doc.set({ test: 'test' })
  await doc.save()
  assert.equal(doc.test, 'test')

  // not working
  doc.set({ data: { sub: 'sub' } })
  await doc.save()
  assert.equal(doc.data.sub, 'sub')

  console.log('done')
})()

which throws this error

/node_modules/mongoose/lib/document.js:2761
      if (!_this.$isValid(path)) {
                 ^

TypeError: _this.$isValid is not a function
    at /node_modules/mongoose/lib/document.js:2761:18
    at processTicksAndRejections (node:internal/process/task_queues:78:11)

The script does not crash if

  • the virtual is removed
  • the virtual is not set
  • doc.data.sub is not set

Expected Behavior

It should not crash, or throw a warning on schema creation if isValid should be considered as a reserved keyword.

@BobBatard BobBatard changed the title isValid virtual generate a crash when updating a nested property isValid virtual generates a crash when updating a nested property Jul 19, 2022
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jul 22, 2022
@vkarpov15 vkarpov15 added this to the 6.4.7 milestone Jul 23, 2022
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

3 participants