如何让 $beforeValidate 等到异步操作完成
how to make $beforeValidate wait till async operation has completed
我希望 $beforeValidate 在更新对象以使其通过验证时等待异步操作完成。但目前 $beforeValidate 完成并拒绝记录,因为它没有等待异步操作完成。
class Label extends Model {
async $beforeValidate() {
if(this.name === undefined){
const res = await axios.get('/getSomeName')
console.log(res.body)
this.name = res.body
}
}
static get jsonSchema () {
return {
type: 'object',
required: ['name'],
properties: {
id: { type: 'integer' },
name: { type: 'string' }
}
}
}
}
现在,当我插入名称未定义的标签时,我可以看到在异步 API 调用甚至完成之前就引发了验证错误
await Label.query().insert({name: undefined})
不幸的是$beforeValidate
是一个同步操作。
你可以在Official Documentation. Even there is an issue上查看一下。
我希望 $beforeValidate 在更新对象以使其通过验证时等待异步操作完成。但目前 $beforeValidate 完成并拒绝记录,因为它没有等待异步操作完成。
class Label extends Model {
async $beforeValidate() {
if(this.name === undefined){
const res = await axios.get('/getSomeName')
console.log(res.body)
this.name = res.body
}
}
static get jsonSchema () {
return {
type: 'object',
required: ['name'],
properties: {
id: { type: 'integer' },
name: { type: 'string' }
}
}
}
}
现在,当我插入名称未定义的标签时,我可以看到在异步 API 调用甚至完成之前就引发了验证错误
await Label.query().insert({name: undefined})
不幸的是$beforeValidate
是一个同步操作。
你可以在Official Documentation. Even there is an issue上查看一下。