这个 NodeJS API 做对了吗? - Chatfuel 没有按预期解析它

is this NodeJS API done correctly? - Chatfuel does not parse it as intended

Chatfuel 不会解析 JSON-API returns 的整个 json 字符串。 欢迎任何帮助。

虽然从 API 返回的 JSON 看起来像这样(在邮递员中):

{
    "messages": [
        {
            "text": "i think you should study"
        },
        {
            "text": "Mikrobiologi"
        }
    ]
}

Messenger bot 只发送第一个文本。

我的应用代码:

router.get('/ask/:question', function(req, res){
  var output = [];
  var keywords = req.params.question.split(' ');
  var answer = qHandler.search(keywords);
  answer.then(function(books){
    output.push({text: 'i think you should study'})
    for (var i = books.length; i > 0; i--){
      output.push({text: books[i-1].title});
      if (i-1 > 0){
        output.push({text: ' and '});
      }
    };
    res.send({messages: output});
  });
});

我试过更改顺序,在返回的字符串前后添加更多硬编码文本。

在邮递员中,一切看起来都应该如此,但 chatfuel 似乎无法解析插入书名的文本对象。

看起来你的代码是正确的。 请确保您在 answer.then(function(books){

下不使用 postman 获取 HTTP request.debug books 值的参数

在这里我可以用纯 javascript

来完成这段代码
var books = [
    {
        title: 'asdfasdf'
    },{
        title: 'asdfasdf'
    },{
        title: 'asdfasdf'
    },{
        title: 'asdfasdf'
    },{
        title: 'asdfasdf'
    }]

var output = [];
output.push({ text: 'i think you should study' })
for (var i = books.length; i > 0; i--) {
    output.push({ text: books[i - 1].title });
    if (i - 1 > 0) {
        output.push({ text: ' and ' });
    }
};
console.log(output);