我的每个语句都出现了问题,其中输出的HTML标签数量远大于我的SQL查询中显示的记录数量。我在测试中能够识别的是,我的SQL查询似乎没有问题,因为它显示了数据库中记录的确切数量,并没有散布出数据。这让我觉得它可能是我的循环或对象存储SQL查询信息。
以下是显示的HTML:
这是HTML:
{{#each currentUsers}}
<li>{{this.email}} ({{#with this.teams.[0]}}{{teamName}}{{else}}No Team{{/with}})|<a href="/app/settings/users/{{this.userId}}/team">Edit Team</a> {{#ifeq this.userId ../user.userId}} (Admin) {{else}}| <a href="/app/users/{{this.userId}}" id="user-delete-link">Delete</a>{{/ifeq}}</li>
{{else}}
<p>No additional users</p>
{{/each}}
这是我的路线:
appRoutes.route('/settings/users')
.get(function(req, res){
models.User.find({
where: {
organizationId: organization.organizationId
}, attributes: ['userId', 'email', 'firstName', 'lastName'],
include: [{
model: models.Team,
attributes: ['teamName']
}]
});
}).then(function(currentUsers){
res.render('pages/app/settings-users.hbs',{
user: req.user,
currentUsers: currentUsers
});
}).catch(function(error){
res.send(error);
console.log(error);
})
});
以下是SQL的输出,它正如我所期望的那样只显示一条记录:
最后是实际的SQL:
SELECT `user`.*, `teams`.`team_id` AS `teams.teamId`, `teams`.`team_name` AS `teams.teamName`, `teams.member`.`member_id` AS `teams.member.memberId`, `teams.member`.`member_email` AS `teams.member.memberEmail`, `teams.member`.`organization_id` AS `teams.member.organizationId`, `teams.member`.`team_id` AS `teams.member.teamId`, `teams.member`.`user_id` AS `teams.member.userId`, `teams.member`.`created_at` AS `teams.member.created_at`, `teams.member`.`updated_at` AS `teams.member.updated_at`, `teams.member`.`organization_id` AS `teams.member.organization_id`, `teams.member`.`team_id` AS `teams.member.team_id`, `teams.member`.`user_id` AS `teams.member.user_id` FROM (SELECT `user`.`user_id` AS `userId`, `user`.`email`, `user`.`first_name` AS `firstName`, `user`.`last_name` AS `lastName` FROM `user` AS `user` WHERE `user`.`organization_id` = 2 LIMIT 1) AS `user` LEFT OUTER JOIN (`member` AS `teams.member` INNER JOIN `team` AS `teams` ON `teams`.`team_id` = `teams.member`.`team_id`) ON `user`.`userId` = `teams.member`.`user_id`;
答案 0 :(得分:0)
尝试使用Model.findAll()
,如果有任何数据,则始终返回对象数组。如果有一个记录,则.find()
返回对象,如果有更多