I have a model:
const message = new mongoose.Schema({
id: { type: ObjectId, required: true },
text: { type: String },
replies: [message]
});
Looking to create a document structure like this:
{
"id": 1,
"text": "Main Message",
"replies": [
{
"id": 11,
"text": "Reply One",
"replies": [
{
"id": 111,
"text": "Second Level Reply",
"replies": []
},
{
"id": 112,
"text": "Another Second Level Reply",
"replies": []
}
]
}
]
}
Based on the solution provided in this link
You can resolve it by utilizing
this
as a reference to the model.
const message = new mongoose.Schema({
id: { type: ObjectId, required: true },
comment: { type: String },
- replies: [comment]
+ replies: [this]
});
However, when attempting this with TypeScript, an error may occur:
'this' implicitly has type 'any' because it does not have a type annotation.
Is there a recommended method to model self-references using mongoose in TypeScript?