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

Non-strict schema properties are not accessible after loading document #10828

Closed
adamreisnz opened this issue Oct 1, 2021 · 1 comment
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@adamreisnz
Copy link
Contributor

adamreisnz commented Oct 1, 2021

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
Properties on a non-strict schema that are accessed directly (e.g. mySchema.prop) yield undefined even though they exist and have values.

Note that I am defining the blank schema like this intentionally, as suggested to me earlier in #10383

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

const mongoose = require('mongoose')
const {Schema} = mongoose
const uri = `mongodb://localhost:27017/test`
console.log(`Mongoose version: ${mongoose.version}`)

const FooSchema = new Schema({}, {
  id: false,
  _id: false,
  strict: false,
})
const BarSchema = new Schema({
  name: String,
  foo: FooSchema,
})

//Model
const Bar = mongoose.model('Bar', BarSchema)

//Test function
async function test() {

  //Connect to database
  await mongoose.connect(uri)

  //Create new item
  const bar = new Bar({
    name: 'Test',
    foo: {
      something: 'A',
      other: 2,
    },
  })

  console.log(bar.foo)
  console.log(bar.foo.something)
  console.log(bar.foo.other)

  await bar.save()
  const {_id} = bar

  //Query
  const barLoaded = await mongoose
    .model('Bar')
    .findOne({_id})
  console.log(barLoaded);
  console.log(barLoaded.foo)
  console.log(barLoaded.foo.something)
  console.log(barLoaded.foo.other)
}

test()

Result:

console.log(bar.foo) //{ something: 'A', other: 2 } ✅
console.log(bar.foo.something) //A ✅
console.log(bar.foo.other) //2 ✅

console.log(barLoaded.foo) //{ something: 'A', other: 2 } ✅
console.log(barLoaded.foo.something) //undefined ❌
console.log(barLoaded.foo.other) //undefined ❌

For some reason, the properties are not accessible/yield undefined when accessed directly.
Using toObject() first works and includes all the properties.

What is the expected behavior?
Expected:

console.log(bar.foo) //{ something: 'A', other: 2 } 
console.log(bar.foo.something) //A
console.log(bar.foo.other) //2
console.log(barLoaded.foo) //{ something: 'A', other: 2 }
console.log(barLoaded.foo.something) //A
console.log(barLoaded.foo.other) //2

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node 16, Mongoose 6.0.7, MongoDB 4.2.8

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Oct 1, 2021
@vkarpov15 vkarpov15 added this to the 6.0.9 milestone Oct 2, 2021
vkarpov15 added a commit that referenced this issue Oct 4, 2021
@adamreisnz
Copy link
Contributor Author

Thank you 🎉

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