![](/img/trans.png)
[英]How do I gain the context I want when using a method from an object in a method of another object?
[英]How do I create a method on a sequelize object that checks if there is an another object created that is associated with it?
抱歉,我的标题可能令人困惑。
所以我有三个用sequelize在javascript中创建的对象。 彼此关联的用户,帖子和投票。 我想在帖子模型上创建一个方法,以检查当前登录的用户是否对此特定帖子进行了投票。 我正在使用Passport模块进行用户登录/注销。
var Votes = db.sequelize.define('votes',{
post_id:{
type: db.Sequelize.INTEGER,
allowNull: false,
unique:"voted_user"
},
user_id:{
type: db.Sequelize.INTEGER,
allowNull: false,
unique:"voted_user"
}
},{
underscored: true
});
为此,您可以使用composite key
的概念,方法是定义unique:"voted_user"
,当我们尝试输入相同的组合时,这将引发错误,
model.Votes.create({ post_id : 1 , user_id : 2 }).then(data => {
// Success
}).catch(err => {
// Error
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.