代码之家  ›  专栏  ›  技术社区  ›  Kamal Panhwar

我在自定义验证中遇到了mongoose calback的问题

  •  0
  • Kamal Panhwar  · 技术社区  · 5 年前

    我正在使用快速验证器,并且已经用mongoose数据库find做了一个自定义验证,现在我不能用消息发送。这是我的代码,虽然进程在出错时停止,但它并没有用Json显示消息,但当用户创建它时,它是show all。

            body('mobile', 'Mobile number is required')
              .custom((value, {req, loc, path}) => {
                User.countDocuments({mobile: value}, function (err, c) {
                  if (err) {
                    console.log('An error happen in db')
                  }
                  if (c > 0) {
                    throw new Error('Mobile already  exists finally')
                  } else {
                    return value
                  }
                })
                return value
              })
              .withMessage('Mobile already exists'),
    

    functions: Beginning execution of "app"
    >  events.js:298
    >        throw er; // Unhandled 'error' event
    >        ^
    >
    >  Error: Mobile already  exists finally
    >      at /Users/kamal/Documents/personal/asghar/codes/functions/app/controllers/users_controller.js:117:23
    >      at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/model.js:4849:16
    >      at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/model.js:4849:16
    >      at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16
    >      at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/model.js:4872:21
    >      at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/query.js:4379:11
    >      at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/kareem/index.js:135:16
    >      at processTicksAndRejections (internal/process/task_queues.js:79:11)
    >  Emitted 'error' event on Function instance at:
    >      at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/model.js:4851:13
    >      at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16
    >      [... lines matching original stack trace ...]
    

    我需要加上if条件 return value 但问题是这里的回调并没有给我带来c的值,在上面它是正确的,甚至由于出错而停止,但我不想引起错误而是想进一步 withMessage('Mobile already exists') 我肯定在回电话时出错了。请提出解决方案

    0 回复  |  直到 5 年前
        1
  •  1
  •   Darvesh    5 年前

    这应该行得通

    body("mobile").custom(value => {
        return User.countDocuments({ mobile: value })
            .then(count => {
                if (count > 0) return Promise.reject("Mobile number already exists");
            })
            .catch(error => console.error(error.message));
    });
    

    documentation

    自定义验证器可能返回承诺来指示异步验证 (将等待),或给予任何价值/拒绝承诺 使用自定义错误消息。注意:如果您的自定义验证器返回

        2
  •  0
  •   Kamal Panhwar    5 年前

    在播放和看过许多文档之后,我通过执行以下操作来解决这个问题,在我的用户模型中,我放入了以下代码,因为我的模型实际上是mongoose模块。

    UserSchema.statics = {
      isValidMobile(mobile) {
        console.log(` Searching obile ${mobile} number `)
        return this.findOne({mobile: mobile}).then((result) => {
          if (result) throw new Error('Mobile Number already exists')
        })
      },
    }
    

    现在在我的验证中,我把下面的行改为上面的行。

            body('mobile', 'Mobile number is required')
              .custom((val) => User.isValidMobile(val))
    
    

    它起作用了,我用整个JSON得到了正确的消息,因为它似乎是定制的,需要正确的true/false回复,所以我的方法只回复true或false,并且它可以很好地处理正确的消息,但是消息是从User model类来的,而不是来自validation,但这是有效的。感谢您的回复。