我正在跟踪官方的文档 && 指示性,但是我找不到任何帮助我的东西。
如果给定的param存在于数据库上,我希望验证。
所以我试着:
app/验证器/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与给定参数进行比较?
发布于 2021-08-10 19:23:44
经过几次艰苦的尝试/错误之后,我偶然发现了这个解决方案。
只需遵循hooks.js内部的内容,并传递以逗号分隔的值,如下所示:
get rules () {
return {
userId: 'required|integer|exists:MyDatabase,MyTable,columntoCompare',
}
}https://stackoverflow.com/questions/68731892
复制相似问题