Mongoose 异步自定义验证未按预期工作

在我的架构中,我正在执行大量异步的自定义验证。但是,验证的行为并不像我期望的那样。即使 promises 以“false”解析,mongoose 仍会继续验证。根据他们的文档,情况不应该如此。


示例架构:


    var questSchema = mongoose.Schema({

      questCategory: {

        type: mongoose.Schema.Types.ObjectId,

        required: true,

        validate: {

          validator: async function (v) {

            await data_verificator.checkIfQuestCategoryExists(v);

          },

          message: (props) => `${props.value} is not a valid quest category id.`,

        },

      },

      fulfillmentPeriod: {

        type: String,

        required: true,

        validate: {

          validator: async function (v) {

            await data_verificator.checkFulfillmentPeriod(this.questCategory, v);

          },

          message: (props) =>

            `${props.value} is an invalid value as it violates the limitations set by the quest category.`,

        },

      },

    })

请注意,自定义验证对于这两个模式字段是异步发生的。该questCategory字段的验证工作得很好。如果 promise 解析为false,则验证失败。但是,该领域并非如此fulfillmentPeriod。即使 promise 解析为false,验证也会成功。


我不确定为什么会出现这种奇怪的行为。如果我将 的验证重写fulfillmentPeriod为如下所示,一切都会再次按预期运行。承诺解析为false导致验证失败。这是为什么?为什么它适用于以下代码但不适用于我上面粘贴的初始代码?那是因为我引用了另一个异步验证的模式字段吗?


validator: async function (v) {

  const result = await data_verificator.checkFulfillmentPeriod(this.questCategory, v);

  return result;

},

以防万一这很重要,该checkFulfillmentPeriod函数如下所示:


const checkFulfillmentPeriod = async function (categoryId, period) {

  const connectionManager = require("../configuration").connectionManager;


  var category = await connectionManager.QuestCategoryModel.findOne({

    _id: categoryId,

    availableFulfillmentPeriods: {

      $elemMatch: {

        period: period,

      },

    },

  });


  if (!category) return false;


  return true;

};

该函数只是检查是否有符合条件的类别。如果是,则返回 true。否则为假。据我所知,问题并非出在这个函数上,而是与 mongoose 的验证有关。


该checkIfQuestCategoryExists函数看起来完全一样,只是查询设置不同。


我在这个问题上花了几个小时,此时我再也看不到任何错误了。


我将不胜感激我能得到的任何帮助/建议!


largeQ
浏览 174回答 2
2回答

长风秋雁

您的验证器缺少 return 语句,因此就像您返回 aPromise<void>并且这不会触发 mongo 的验证。您可以添加返回或重写您的函数,并承诺后者不太优雅。new Promise( (resolve,reject) => {&nbsp; .....&nbsp; resolve(true/false);});

月关宝盒

你能试试这段代码吗:var questSchema = mongoose.Schema({&nbsp; &nbsp; &nbsp; questCategory: {&nbsp; &nbsp; &nbsp; &nbsp; type: mongoose.Schema.Types.ObjectId,&nbsp; &nbsp; &nbsp; &nbsp; required: true,&nbsp; &nbsp; &nbsp; &nbsp; validate: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validator: async function (v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return await data_verificator.checkIfQuestCategoryExists(v);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message: (props) => `${props.value} is not a valid quest category id.`,&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; fulfillmentPeriod: {&nbsp; &nbsp; &nbsp; &nbsp; type: String,&nbsp; &nbsp; &nbsp; &nbsp; required: true,&nbsp; &nbsp; &nbsp; &nbsp; validate: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validator: async function (v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return await data_verificator.checkFulfillmentPeriod(this.questCategory, v);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message: (props) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `${props.value} is an invalid value as it violates the limitations set by the quest category.`,&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; })
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript