我有一个问题,我使用Sequelize来运行查询,然后在第一个语句中返回对象,然后在另一个表上运行查询。在运行该查询之后,我有一个额外的then语句,该语句应该传递返回的对象以便能够在我的视图中访问。不幸的是,第二个查询不返回对象,因为控制台日志返回undefined
第二个日志(Check console.log(member)
)。出现这种情况的原因或我做错了什么?查询带有正确的陈述。
appRoutes.route('/settings/add-users')
.get(function(req, res){
models.Organization.find({
where: {
organizationId: req.user.organizationId
}, attributes: ['organizationId', 'organizationName','admin','members']
}).then(function(organization, member){
models.Member.findAll({
where: {
organizationId: organization.organizationId
}, attributes: ['memberEmail']
});
return organization;
}).then(function(organization, member){
console.log(organization);
console.log(member);
res.render('pages/app/add-users.hbs',{
user: req.user,
organization: organization,
member: member
});
}).catch(function(error){
res.send(error);
console.log(error);
})
});
答案 0 :(得分:1)
承诺只能解析为单个值。您将从当时再次返回organization
,因此下一个then
会将其作为其值。您可以使用对象来实现所需的行为。
appRoutes.route('/settings/add-users')
.get(function(req, res){
var organization;
models.Organization.find({
where: {
organizationId: req.user.organizationId
}, attributes: ['organizationId', 'organizationName','admin','members']
}).then(function(_organization){
organization = _organization;
return models.Member.findAll({
where: {
organizationId: organization.organizationId
}, attributes: ['memberEmail']
});
}).then(function(member){
console.log(organization);
console.log(member);
res.render('pages/app/add-users.hbs',{
user: req.user,
organization: organization,
member: member
});
}).catch(function(error){
res.send(error);
console.log(error);
})
});