Typescript Mongoose Middleware - 输入“post”的参数回调函数

发布于 2025-01-11 17:01:41 字数 808 浏览 0 评论 0 原文

使用打字稿和猫鼬: 做了一些阅读,无法弄清楚发生了什么...我正在添加猫鼬中间件来自定义重复的错误消息,但回调的参数未输入。正如您在下面看到的,我必须强制使用any类型来解决隐式的any错误。

// 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

我尝试使用 Query 传递泛型, ProductSchema.post>(...)

如果我删除 doc 参数,错误就会消失但代码不会在重复的文档上执行。

任何建议都会很棒!如果可以的话我想把它打印出来......!感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找一个思念的角度 2025-01-18 17:01:42

我遇到了类似的问题,虽然有点晚了,但这个答案可以为其他人节省时间:

在 6.8 以上的 Mongoose 版本中,您可以将 { errorHandler: true } 添加到“help TypeScript out" 和 error:any 跳过 Property 'code'类型“NativeError”错误不存在。

ProductSchema.post("save", { errorHandler: true }, function (error: any, doc, next) {
  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();
  }
});

最后,“unique”不是 Mongoose 中的验证器,一个问题是它需要字段索引才能工作 文档。因此,在模型中,您可以添加 ProductSchema.index({ "name": 1, "category": 1, "subcategory": 1}, { "unique": true });

I had a similar issue, and although a little bit late this answer can save time for others:

In Mongoose versions above 6.8 you can add { errorHandler: true } to "help TypeScript out" and error:any to skip Property 'code' does not exist on type 'NativeError' error.

ProductSchema.post("save", { errorHandler: true }, function (error: any, doc, next) {
  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();
  }
});

And finally, "unique" is not a validator in Mongoose, a gotcha is that it requires a field index to work docs. So, in the model, you can add ProductSchema.index({ "name": 1, "category": 1, "subcategory": 1}, { "unique": true });

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文