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

Issue accessing subdocument contents from custom setter #5363

Closed
asabhaney opened this issue Jun 14, 2017 · 1 comment
Closed

Issue accessing subdocument contents from custom setter #5363

asabhaney opened this issue Jun 14, 2017 · 1 comment
Milestone

Comments

@asabhaney
Copy link

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:

const mongoose = require('mongoose')
const Schema = mongoose.Schema

// The subdocument schema
const contentSchema = new Schema({
   blocks: [{ type: String }],
   summary: { type: String }
})

// Subdocument setter
contentSchema.path('blocks').set(function (srcBlocks) {
   const content = this

   // The issue is most obvious when the document is being updated, 
   // so we only focus on the case where the doc is being updated
   if (!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 empty
      console.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'))
   }

   return srcBlocks
})

// Normal schema that uses the above subdocument schema as single nested subdocs
const noteSchema = new Schema({
   title: { type: String, required: true },
   body: { type: contentSchema }
})

const Note = mongoose.model('note-subdoc-test', noteSchema)

// Create a new Note, this works fine
const note = new Note({
   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 is
   note.set('body', {
      summary: 'New Summary',
      blocks: ['gallery', 'html']
   })

   return note.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!

@vkarpov15 vkarpov15 added this to the 4.10.8 milestone Jun 16, 2017
@vkarpov15
Copy link
Collaborator

Thanks for the detailed repro script, will investigate for next patch release.

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