You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to report (what I believe to be) a bug, though it's certainly possible I'm trying to do something that is unsupported or that I've misunderstood something. I'm doing some work with single-nested subdocuments, and running in to an issue when I attempt to access the contents of a subdocument from a setter that I've added to the subdoc's schema.
The reason I want to have the custom setter is to be able to access the current value of that property, and set the new value of that property accordingly.
I've written a simple script that reproduces the issue:
constmongoose=require('mongoose')constSchema=mongoose.Schema// The subdocument schemaconstcontentSchema=newSchema({blocks: [{type: String}],summary: {type: String}})// Subdocument settercontentSchema.path('blocks').set(function(srcBlocks){constcontent=this// The issue is most obvious when the document is being updated, // so we only focus on the case where the doc is being updatedif(!content.ownerDocument().isNew){// Ideally I would return something here depending on the current // value of `content.blocks`// The issue is here! It seems that accessing `this` shows// the subdocument to be emptyconsole.log('Normal way',content)// ...However, if we try to access the subdocument this way,// the subdocument looks just fine!console.log('Hack way',content.ownerDocument().get('body'))}returnsrcBlocks})// Normal schema that uses the above subdocument schema as single nested subdocsconstnoteSchema=newSchema({title: {type: String,required: true},body: {type: contentSchema}})constNote=mongoose.model('note-subdoc-test',noteSchema)// Create a new Note, this works fineconstnote=newNote({title: 'Lorem Ipsum Dolor',body: {summary: 'Summary Test',blocks: ['html']}})note.save().then(note=>Note.findById(note.id)).then((note)=>{// This triggers the custom setter, which is where the issue isnote.set('body',{summary: 'New Summary',blocks: ['gallery','html']})returnnote.save()})
The output of this snippet is:
Normal way { _id: 5941a9d827dd6fb24f82881c, blocks: [] }
Hack way { summary: 'Summary Test',
_id: 5941a9d627dd6fb24f82881a,
blocks: [ 'html' ] }
(Even the _id is different - odd!)
Using version 4.10.6 of mongoose and version 6.11.0 of node.js. Thanks in advance!
The text was updated successfully, but these errors were encountered:
Thanks for all the great work on mongoose.
I'd like to report (what I believe to be) a bug, though it's certainly possible I'm trying to do something that is unsupported or that I've misunderstood something. I'm doing some work with single-nested subdocuments, and running in to an issue when I attempt to access the contents of a subdocument from a setter that I've added to the subdoc's schema.
The reason I want to have the custom setter is to be able to access the current value of that property, and set the new value of that property accordingly.
I've written a simple script that reproduces the issue:
The output of this snippet is:
(Even the
_id
is different - odd!)Using version 4.10.6 of mongoose and version 6.11.0 of node.js. Thanks in advance!
The text was updated successfully, but these errors were encountered: