Using typescript and mongoose: Been doing a bit of reading and can not figure out what is going on... I am adding mongoose middle ware to customize a duplicate error message, but the arguments of the callback are not typed. As you can see below, I have to force the any type to resolve the implicit any error.
// Define Schema...
// Then call this:
ProductSchema.post("save", function (error: any, doc: any, next: any) {
if (error.code === 11000 && error.name === "MongoServerError") {
next(
new ApolloError(
"A product with this name, category, and subcategory already exists. Please add it to your kit instead of creating it.",
"DUPLICATE_PRODUCT"
)
);
} else {
next();
}
});
// I then call the .model() method after this
I have tried passing generics with Query, ProductSchema.post<Query<Product, Product>>(...)
If I remove the doc argument, the errors then leave but the code does not execute on duplicate documents.
Any suggestions would be great! I'd like to have it typed if possible...! Thanks for your help.