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

[BUG] Strange behaviour with set function #7585

Closed
Zwimber opened this issue Mar 5, 2019 · 0 comments
Closed

[BUG] Strange behaviour with set function #7585

Zwimber opened this issue Mar 5, 2019 · 0 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Zwimber
Copy link

Zwimber commented Mar 5, 2019

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

What is the current behavior?

  1. Modifying the object in setters behaves strangely
  2. Fields created in set functions are not added to the object

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

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

const NameSchema = new Schema({

    full: {
        type: String,
        set: function (v, x, y) {

            // One way to set values
            this.set('first', 'XXX')

            // Another way
            this.last = 'YYY';

            // Something weird
            this.weird = 'ZZZ'

            // Log current value
            console.log('Current value', this) // { first: 'XXX', last: 'YYY' } // Logs this on each occasion

            return v + ' BBB';
        }
    },
    first: String,
    last: String,
    
}, { _id: false, strict: false })

const User = mongoose.model('user', new Schema({

    name: {
        type: NameSchema,
        default: {},
    }

}, { _id: false, strict: false }))

test('(0) User set name', done => {

    const s = new User()
    s.name = { full: 'AAA', other: 'ZZZ' }
    console.log(s) // { name: { full: 'AAA BBB', other: 'ZZZ' } }
    done()
})

test('(1) User set name', done => {
    const s = new User({ name: { full: 'AAA', other: 'ZZZ' } })
    console.log(s) // { first: 'XXX', last: 'YYY', full: 'AAA BBB', other } }
    done()
})

test('(2) User set name', done => {
    const s = new User()
    // s.name = {}
    s.name.full = 'AAA'
    s.name.other = 'ZZZ';
    console.log(s) // { first: 'XXX', last: 'YYY', full: 'AAA BBB' } }
    done()
})

Result in a table:

name.full name.first name.last name.weird name.other
Test 0 AAA BBB - - - ZZZ
Test 1 AAA BBB ZZZ YYY - ZZZ
Test 2 AAA BBB ZZZ YYY - -

What is the expected behavior?

Firstly I expect test (0) to produce the same result as test (1) and test (2). Please note that everything works very differently if you comment out default: {} property (and uncomment //s.name = {}.

Secondly I'd expect that on each of the tests the property weird to exist.

Please mention your node.js, mongoose and MongoDB version.

Node: 11.10.0
Mongoose: 5.4.17
MongoDB: 3.6.9

@vkarpov15 vkarpov15 added this to the 5.4.20 milestone Mar 6, 2019
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Mar 6, 2019
vkarpov15 added a commit that referenced this issue Mar 24, 2019
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Mar 24, 2019
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

2 participants