【发布时间】:2015-08-24 08:53:46
【问题描述】:
我正在尝试获取属于特定公司的用户数。
这是我的模型;
var Company = Bookshelf.Model.extend({
tableName: 'companies',
users: function () {
return this.hasMany(User.Model, "company_id");
},
users_count : function(){
return new User.Model().query(function(qb){
qb.where("company_id",9);
qb.count();
}).fetch();
},
organization: function () {
return this.belongsTo(Organization.Model, "organization_id");
}
});
- 方法“users”效果很好,没问题。
- 方法“users_count”查询效果很好,但无法获得“公司”模型的价值。
在路线中,我正在使用这样的书架模型;
new Company.Model({id:req.params.id})
.fetch({withRelated:['users']})
.then(function(model){
res.send(model.toJSON())
})
.catch(function(error){
res.send(error);
});
我应该如何使用 users_count 方法,我有点困惑(可能是因为承诺)
【问题讨论】:
标签: node.js postgresql relational-database bookshelf.js