2016-08-23 53 views
0

我的手柄條每個語句都有問題,輸出HTML標籤的數量遠遠大於我的SQL查詢中顯示的記錄數量。我在測試中能夠識別的是,我的SQL查詢似乎沒有問題,因爲它顯示數據庫中記錄的確切數量,並且不會扇出數據。這讓我認爲它可能是我的循環或對象存儲SQL查詢信息的東西。每把輸出多於記錄的把手

這裏正在顯示HTML:

handlebars output

下面是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,它顯示了一條記錄a的I期待:

SQL Output

最後實際的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

嘗試使用'Model.findAll()',如果有任何數據總是返回對象數組。 '.find()'返回對象,如果有一個記錄和數組,如果更多 –

+0

非常感謝你@TilekbekovYrysbek,這是我正在尋找的確切答案。您是否將此貼爲答案,我會批准並贊成?非常有用和簡單的修復! – cphill

回答

0

嘗試使用Model.findAll()總是返回一個對象,如果有任何數據的陣列。 .find()返回對象如果有一個記錄和數組如果更多