gpt4 book ai didi

node.js - Sequelize.js - 异步验证器

转载 作者:搜寻专家 更新时间:2023-10-31 23:33:17 25 4
gpt4 key购买 nike

是否可以使用 Sequelize.js 进行异步验证?我想在保存模型之前检查关联是否存在。像这样:

User = db.define("user", {
name: Sequelize.STRING
},
{
validate:
hasDevice: ->
@getDevices().success (devices) ->
throw Exception if (devices.length < 1)
return
})

# .... Device is just another model

User.hasMany(Device)

或者有没有办法强制该检查同步运行? (不理想)

最佳答案

您可以在 v2.0.0 中使用异步验证。它是这样工作的:

var Model = sequelize.define('Model', {
attr: Sequelize.STRING
}, {
validate: {
hasAssociation: function(next) {
functionThatChecksTheAssociation(function(ok) {
if (ok) {
next()
} else {
next('Ooops. Something is wrong!')
}
})
}
}
})

关于node.js - Sequelize.js - 异步验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19640308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com