-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Map in EmbededDocument, the set function error! #10295
Comments
Just a heads up your 2nd console log is incorrect, firstMap.first.data.second didn't show up for me in there but it did on the next one |
Original issue is: typegoose/typegoose#541 Slightly modified version (hopefully better to understand): Code// NodeJS: 16.1.0
// MongoDB: 4.2-bionic (Docker)
import * as mongoose from "mongoose"; // [email protected]
import { inspect } from "util";
const SecondMapSchema = new mongoose.Schema({
data1: {
_id: false,
default: {},
type: Map,
of: Number
}
});
const FirstMapSchema = new mongoose.Schema({
data2: {
_id: false,
default: {},
type: Map,
of: SecondMapSchema
}
});
const NestedSchema = new mongoose.Schema({
data3: {
_id: false,
default: {},
type: Map,
of: SecondMapSchema
}
});
const TestSchema = new mongoose.Schema({
firstMap: {
_id: false,
default: {},
type: Map,
of: FirstMapSchema
},
nested: {
_id: false,
default: {},
type: NestedSchema
}
});
const TestModel: any = mongoose.model("Test", TestSchema);
(async () => {
await mongoose.connect(`mongodb://localhost:27017/`, { useNewUrlParser: true, dbName: "verifyMASTER", useCreateIndex: true, useUnifiedTopology: true });
const doc = await TestModel.create({});
console.log(inspect(doc, false, 5));
console.log("Should be empty", doc.modifiedPaths());
// Ok!
doc.firstMap.set("first", {});
console.log("Should include firstMap.first", doc.modifiedPaths());
await doc.save();
// Ok!
doc.firstMap.get("first").data2.set("second", {});
console.log("Should include firstMap.first.data2.second", doc.modifiedPaths());
await doc.save();
// Ok!
doc.firstMap.get("first").data2.get("second").data1.set("final", 3);
console.log(
"Should include firstMap.first.data2.second.data1.final",
doc.modifiedPaths()
);
await doc.save();
// Ok!
doc.nested.data3.set("second", {});
console.log("Should include nested.data3.second", doc.modifiedPaths());
await doc.save();
// ERROR!
console.log("test1", doc.nested.data3);
doc.nested.data3.get("second").data1.set("final", 3);
console.log("test2", doc.nested.data3);
console.log(
"Should include nested.data3.second.data1.final",
doc.modifiedPaths()
);
await doc.save();
console.log(inspect(doc, false, 5));
// This is OK, but have to set "nested" to "{}"
const okDoc = await TestModel.create({ nested: {} });
okDoc.nested.data3.set("second", {});
await okDoc.save();
okDoc.nested.data3.get("second").data1.set("final", 3);
console.log(
"Should include nested.data3.second.data1.final",
okDoc.modifiedPaths()
);
await okDoc.save();
await mongoose.disconnect();
})(); Console Output
Reproduction repository (with correct branch): https://github.com/typegoose/typegoose-testing/tree/verify541 (including original typegoose classes) |
@IslandRhythms Sorry! I fixed the description in the second console. |
Do you want to request a feature or report a bug?
bug
What is the current behavior?
define a two level Map field in a NestedDocument, when call the inner Map's function (set), it will not automatically add the modified path to the path list.
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
The behavior is written in the code's comment.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: v12.16.3
Mongo: 4.4.6
Mongoose: both 5.10.18 and 5.12.11 are tested and failed
The text was updated successfully, but these errors were encountered: