#How to include static method in the model interface?

28 messages · Page 1 of 1 (latest)

minor hemlock
#

@subtle inlet
could you help sir?

subtle inlet
#

I think you are fighting things. How can you have $model on this at all, when this is the review document?

minor hemlock
#

I didn't understand....

subtle inlet
#

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.

minor hemlock
#

doesn't the $model method return another model of the same connection?

subtle inlet
#

$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.

minor hemlock
#

okay

#

but...

#

how did it work?

subtle inlet
#

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>
minor hemlock
#

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?

subtle inlet
#

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.

minor hemlock
#

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
subtle inlet
#

Seems Product is undefined.

minor hemlock
#

I have imported it

#

and it is working everywhere else

#

when I do Ctrl + click on product, it also takes me to that schema

subtle inlet
#

What is your Editor? Try restarting it. Otherwise, I have no clue.

minor hemlock
#

It didn't fix, so I renamed Product.name to "Product"

minor hemlock
#

@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.