#How to include static method in the model interface?
28 messages · Page 1 of 1 (latest)
I think you are fighting things. How can you have $model on this at all, when this is the review document?
I didn't understand....
When the review post save hook is ran, this is the review document that savewas called on. There is no product.model in the document object.
doesn't the $model method return another model of the same connection?
I don't even see a $model method on the document. https://mongoosejs.com/docs/api/document.html
$model is on the Model object.
So, I refer back to what I said earlier. Do this logic inside your service. Using hooks won't work for this use case. Sorry, I mentioned it.
I hve no idea. You said it didn't work.
From a TypeScript perspective, you casted Model onto this. Which is wrong.
const Review = this.constructor as Model<ReviewDocument>
ohh, I am just a beginner; thanks for help :)
anyways, now I have to remove this middleware
should I call the static method in the product service?
or can I call this inside a virtual of the product schema?
No. You need to take the result and call your product service to save it in your review service. Remove the static method altogether.
Static methods are only there to create new methods to work with the document of that entity, not other entities.
oh, got it
also why am I getting this type-error?
TypeError: Cannot read properties of undefined (reading 'name')
@Prop({ type: Types.ObjectId, ref: Product.name, required: true })
product: Product
Seems Product is undefined.
I have imported it
and it is working everywhere else
when I do Ctrl + click on product, it also takes me to that schema
What is your Editor? Try restarting it. Otherwise, I have no clue.
It didn't fix, so I renamed Product.name to "Product"
@subtle inlet I found the root of this error
In the Product class' file, I was importing the Review class, so the Review class was being called first.
As a resut, the Product wouldn't be defined at that time.
I simply removed that import statement of that Product class file, and the error was gone.