Fieldbook Codelet - 不返回所有匹配的记录

时间:2017-07-18 16:07:22

标签: javascript json node.js for-loop

我在slackbot的fieldbook中创建了一个codelet。它旨在返回与松弛请求的日期匹配的所有记录。现在它被设置为仅返回第一条记录,但我需要所有这些记录。我对此很新,如果这是一个基本问题,我道歉,但任何帮助都会很棒。以下是我到目前为止的情况。

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,
       }]
     }
   })
 }

1 个答案:

答案 0 :(得分:0)

我相信你的问题与迭代for循环中变量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,
            }]
        }
    }
})

希望这有帮助!