2017-07-18 70 views
0

我正在爲slackbot的字段中創建一個小碼。它意味着返回與鬆弛中請求的日期相匹配的所有記錄。現在它被設置爲只返回第一個記錄,但我需要所有的記錄。我對此很新,我很抱歉如果這是一個基本問題,但任何幫助都會很棒。以下是我到目前爲止。字段小程序 - 不返回所有匹配的記錄

var _ = require('underscore'); 
var s = require('underscore.string'); 

exports.endpoint = exports.endpoint = function (request, response) { 
    var jobDate = request.body.text; 
    // Get the date from Job Notes 
    var date = `${(jobDate)}`; 

    // Find all records with the given date 
    var query = {date: date}; 
    return client.list('job_notes', query).then(function (records) { 
     // This is only pulling the first record 

我需要所有匹配日期的記錄,這是我相信我需要一個for循環的地方。

 var record = records[0]; 

     if (!record) return `No data found for ${jobDate}`; // Did not match any record 

     var employee = record.employee[0]; 
     var job = record.job[0]; 

     var attributes = [ 
     {title: 'Date', value:date, short: true}, 
     {title: 'Time', value:record.time, short: true}, 
     {title: 'Employees', value:employee, short: true}, 
     {title: 'Job', value:job, short: true}, 
     {title: 'Note', value:record.note, short: true}, 
     ]; 

     return { 
     attachments: [{ 
      fallback: record.name, 
      title: record.name, 
      fields: attributes, 
     }] 
    } 
    }) 
} 

回答

0

我相信你的問題已經通過在變量records在通過了循環的項目列表迭代做。這可以通過以下代碼完成:

return client.list('job_notes', query).then(function(records) { 
    for (i = 0; i < records.length; i++) { 
     var record = records[i]; 

     if (!record) return `No data found for ${jobDate}`; // Did not match any record 

     var employee = record.employee[0]; 
     var job = record.job[0]; 

     var attributes = [ 
      {title: 'Date', value:date, short: true}, 
      {title: 'Time', value:record.time, short: true}, 
      {title: 'Employees', value:employee, short: true}, 
      {title: 'Job', value:job, short: true}, 
      {title: 'Note', value:record.note, short: true}, 
     ]; 


     return { 
      attachments: [{ 
       fallback: record.name, 
       title: record.name, 
       fields: attributes, 
      }] 
     } 
    } 
}) 

希望這有助於!

+0

謝謝,我會試試這個,讓你知道。我很欣賞快速反應! – chasewarner

相關問題