Mongoose: 銆怋ug at v5.3.15+銆慣he nested Map cannot be read?

Created on 22 Mar 2019  路  2Comments  路  Source: Automattic/mongoose

Schema

const schema = new mongoose.Schema({
    describe: {
        type: Map,
        of: Map,
        default: {}
    }
});

const GoodsInfo = mongoose.model('GoodsInfo', schema);

At v5.3.14

(async function(){
    try {
        let goodsInfo = new GoodsInfo();
        goodsInfo.describe = new Map();
        goodsInfo.describe.set('brand', new Map([['en', 'Hermes']]));

        await goodsInfo.save();
        const id = goodsInfo.id;
        console.log(id);

        goodsInfo = await GoodsInfo.findById(id);
        console.log(goodsInfo.describe); // => Map(1)聽{"brand" => Map(1)}
    } catch (err) {
        console.error('ERROR :', err);
    }
})()

As expected
3

At v5.3.15+

(async function(){
    try {
        let goodsInfo = new GoodsInfo();
        goodsInfo.describe = new Map();
        goodsInfo.describe.set('brand', new Map([['en', 'Hermes']]));

        await goodsInfo.save();
        const id = goodsInfo.id;
        console.log(id);

        goodsInfo = await GoodsInfo.findById(id);
        console.log(goodsInfo.describe); // => Map(0) {}
    } catch (err) {
        console.error('ERROR :', err);
    }
})()

I get one empty Map
1

At v5.3.15+ modify Schema

const schema = new mongoose.Schema({
    describe: {
        type: Map,
        of: Object, // Map change to Object
        default: {}
    }
});

Non-nested Map schema for correct reading
2

confirmed-bug

Most helpful comment

I also verified that when i manually revert the few changes made in https://github.com/Automattic/mongoose/commit/07fc01757640be6a071f299e6d40358f8d8a3e22 , the code works as expected, even in v5.4.19.
Don't have time right now to dive into this in more detail, but i think this should be doable very easy, since the commit only contains a hanful of new lines and changes.
Will try to see, if i can fix this later today.

All 2 comments

I can confirm this. v5.3.14 works correct, 5.4.19 (latest) for example doesn't work.
I also checked the values stored in the DB with MongoDB-Compass and the man is not stored.
Did we maybe miss any breaking changes?

I also verified that when i manually revert the few changes made in https://github.com/Automattic/mongoose/commit/07fc01757640be6a071f299e6d40358f8d8a3e22 , the code works as expected, even in v5.4.19.
Don't have time right now to dive into this in more detail, but i think this should be doable very easy, since the commit only contains a hanful of new lines and changes.
Will try to see, if i can fix this later today.

Was this page helpful?
0 / 5 - 0 ratings