【问题标题】:AdonisJS exists ValidatorAdonisJS 存在验证器
【发布时间】:2021-10-14 07:37:37
【问题描述】:

我正在关注官方 [文档] (https://legacy.adonisjs.com/docs/4.0/validator) && indicative,但我找不到任何可以帮助我的东西。

我想验证给定的参数是否存在于数据库中

所以我尝试了:

app/Validators/myValidator

const { rule } = use('Validator')

get rules () {
    return {
      userId: 'required|integer|exists:MyDatabase.users,userId', <-- this is what isn't working
      date: [
        rule('date'),
        rule('dateFormat', 'YYYY-MM-DD')
      ]
    }
  }

// Getting the data to be validated
  get data () {
    const params = this.ctx.params

    const { userId, date } = params

    return Object.assign({}, { userId }, { date })
  }

它给了我以下错误:

{
  "error": {
    "message": "select * from `MyDatabase`.`users`.`userId` where `undefined` = '2' limit 1 - ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.`userId` where `undefined` = '2' limit 1' at line 1",
    "name": "ErrorValidator",
    "status": 40
  }
}

我应该如何正确表明我想将MyDatabase.users.userid 与给定参数进行比较?

【问题讨论】:

    标签: adonis.js indicative


    【解决方案1】:

    经过几次努力尝试/错误后,我偶然发现了解决方案。
    只需按照hooks.js 中的内容传递以逗号分隔的值,如下所示:

    get rules () {
      return {
        userId: 'required|integer|exists:MyDatabase,MyTable,columntoCompare',      
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      相关资源
      最近更新 更多