2015-02-24 24 views
0

爲什麼這不顯示所有通道。我如何顯示所有頻道。如何使用bookshelfjs顯示所有記錄

new channelModel() 
    .fetch() 
    .then(function (channel) { 
     console.log(channel.attributes.name); 
     if (channel) { 
      res.json({error: false, status: 200, data: channel.attributes.name}); 
     } else { 
      res.json({error: true, status: 404, data: 'channel does not exist'}); 
     } 
    }) 
    .otherwise(function (err) { 
     res.status(500).json({error: true, data: {message: err.message}}); 
    }); 

任何想法?

回答

0

使用fetchAll

new channelModel() 
    .fetchAll() 
    .then(function (channels) { 
     channels.forEach(function(channel) { 
      // do something with each channel 
     }); 
     // or just respond with JSON array assuming this is an Express app: 
     res.send(channels.toJSON()); 
    }) 

我還沒有真正嘗試過的forEach功能,但according to bookshelf.js documentation它應該工作。如果您正在執行REST服務,您可能只想將集合(數組)作爲JSON序列化。