Lodash _.set or Object.defineProperty is not working with subschema
See original GitHub issuePrerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
6.1.7
Node.js version
14.15.4
MongoDB server version
4.3.1
Description
When using lodash to set a property in a sub schema, mongoose don’t detect it as update and when doing a save
, the object is not updated. After deeping in lodash, it seems that it’s because we are using Object.defineProperty
.
https://github.com/lodash/lodash/blob/master/.internal/baseAssignValue.js#L10
Steps to Reproduce
/* eslint-disable no-console */
const mongoose = require('mongoose');
const _ = require('lodash');
main().catch((err) => console.log(err));
async function main() {
const blockSchema = new mongoose.Schema({
type: String,
value: String,
}, { _id: false });
const descriptionSchema = new mongoose.Schema({
en: [blockSchema],
fr: [blockSchema],
}, { _id: false });
const mainSchema = new mongoose.Schema({
meta: {
message: descriptionSchema,
},
});
const Main = mongoose.model('Main', mainSchema);
await mongoose.connect('mongodb://localhost:27017/test');
const foo = await Main.create({
meta: {
message: {
fr: [
{ type: 'text', value: 'Ceci est un test bar' },
],
},
},
});
const fooNewly = await Main.findById(foo._id);
const description = fooNewly.meta.message;
// *************************
// You can use the lodash version or the Object version as you want
// Version 1
_.set(description, 'en', [
{ type: 'text', value: 'This is a test zoz' },
]);
_.set(fooNewly, 'meta.message', description);
// Verison 2
/*
Object.defineProperty(description, 'en', {
configurable: true,
enumerable: true,
value: [
{ type: 'text', value: 'This is a test zoz' },
],
writable: true,
});
Object.defineProperty(fooNewly, 'meta.message', {
configurable: true,
enumerable: true,
value: description,
writable: true,
});
*/
// Working version
/*
description.en = [
{ type: 'text', value: 'This is a test zoz' },
];
fooNewly.meta.message = description;
*/
// *************************
console.info({ modifiedPaths: fooNewly.modifiedPaths() });
await fooNewly.save();
const fooUpdated = await Main.findById(foo._id);
console.info({ objectFr: fooUpdated.meta.message.fr.toObject() });
console.info({ objectEn: fooUpdated.meta.message.en.toObject() });
}
Expected Behavior
The mongoose model should be updated and the path should be mark as modified to be able to be saved.
Issue Analytics
- State:
- Created a year ago
- Comments:5
Top Results From Across the Web
Object.defineproperty() set not working as expected ...
You create infinite loop with set method because inside you try to set property with same name so the same set method is...
Read more >Calling lodash _.defaults() before Object.defineProperty()
Should I expect there to be problems calling defineProperty after the property has already been merged in with _.defaults() ? I don't see...
Read more >Unexpected behaviour _.assign with getter/setter · Issue #3488
If I have a getter defined on my destination object, when assigning a source object the getter will not get overridden by the...
Read more >mongoose/History.md - UNPKG
mongoose/History.md ; 3, * fix(schema): check that schema type is an object when setting isUnderneathDocArray #10361 [vmo-khanus](https://github.com/vmo-khanus).
Read more >Analysis Report WUPOS_Service_x64.exe - Joe Sandbox
Found dropped PE file which has not been started or loaded ... Object.defineProperty(exports, "__esModule", { value: true });.const util ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
i think
defineProperty
is not detectable like aget
/set
would be, and could only be detected when (forcibly) comparing a saved state with the current state, which is a approach mongoose currently does not use (from what i know)the workaround for now would be to manually mark it as modified
doc.markModified('path.to.property')
(in your casefooNewly.markModified('meta.message')
)This issue was closed because it has been inactive for 19 days since being marked as stale.