I have a model:
const comment = new mongoose.Schema({
id: { type: ObjectId, required: true },
comment: { type: String },
replies: [comment]
});
Want to create document like this:
{
"id": 1,
"comment": "Grand Parent Comment",
"replies": [
{
"id": 11,
"comment": "Parent Comment",
"replies": [
{
"id": 111,
"comment": "Comment",
"replies": [
{
"id": 1111,
"comment": "Child Comment",
"replies": []
}
]
},
{
"id": 112,
"comment": "Sibling Comment",
"replies": []
}
]
}
]
}
According to the answer to this answer
Can be solved just by using
this
as a reference to the model.
const comment = new mongoose.Schema({
id: { type: ObjectId, required: true },
comment: { type: String },
- replies: [comment]
+ replies: [this]
});
When i try with Typescript an error will appear like:
'this' implicitly has type 'any' because it does not have a type annotation.
Any best practice to model self reference with mongoose writing on typescript.