-
-
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
findByIdAndUpdate is NOT Callable typescript (fails to execute on union type of Models) #10305
Comments
can you provide allModels? |
@DVGY without seeing what |
@vkarpov15 @IslandRhythms Here is the codesandbox Checkout the file Thanks |
|
Unfortunately this looks like a limitation in TypeScript: microsoft/TypeScript#33591 . TS doesn't handle this sort of union type behavior well, we would recommend defining this as a static instead. reviewsSchema.static('expressUpdateOne', async function(
req: express.Request,
res: express.Response,
next: express.NextFunction
): Promise<void> {
const Model = this;
const { id } = req.params;
const doc = await Model.findByIdAndUpdate(id, req.body, {
new: true,
runValidators: true
});
const doc2 = await userDefinedModel.find();
const doc3 = await Model.findByIdAndUpdate();
const modelName = Model.modelName;
if (!doc) {
// Error
}
res.status(200).json({
status: "success",
[modelName]: { doc }
});
}); |
@vkarpov15 this update one will run of different models too, I see here you are registering it on reviewSchema. Can I register it on Model ? |
@DVGY sure, you can structure it as a global plugin: https://mongoosejs.com/docs/plugins.html |
I got the same error and found out it was related to the model export. |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
This expression is not callable.
findByIdAndUpdate
If the current behavior is a bug, please provide the steps to reproduce.
I can share my private repo with the member of this repo. Let me know.
What is the expected behavior?
findByIdAndUpdate
should be callablefind
should be callableIf I replace userDefinedModel with specific model like
Users.findByIdAndUpdate
orUsers.find()
, it works.What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: 14.15.4, "mongoose": "^5.12.12", MongoDB 4.4
The text was updated successfully, but these errors were encountered: