繁体   English   中英

使用 Alexa-SDK 如何让 this.emit() 方法返回 JSON 数组中的多个对象?

[英]Using the Alexa-SDK how can I get this.emit() method to return multiple objects in a JSON array?

我正在尝试使用 emit() 方法从 JSON 数组中获取多个帐户 ID。 发射方法是从 Alexa-SDK 定义的。 问题是它只发出一个值而不是全部。 我怎样才能让emit()方法返回一个JSON数组中的两个对象值?

这是我的代码片段:

'use strict';

const Alexa = require('alexa-sdk');

const handlers = {
  'LaunchRequest': function() {
    this.emit(':tell', 'sure');
    this.emit('getEmployeeInfoIntent');
  },
  'getEmployeeInfoIntent': function() {
    var empinfoData = {
    "employees": [{
        "account_id": 8675309
      },{
        "account_id": 54321
      }]
    };
    for (var i in empinfoData) {
      var employeeInfo = empinfoData[i].account_id;
      this.emit(':tell', 'The accounts available are id number' + employeeInfo);
      //return only 8675309 but I want 8675309 and 54321
    };
  }
}; ///end of handler

exports.handler = (event, context) => {
  const alexa = Alexa.handler(event, context);
  alexa.APP_ID = APP_ID;
  alexa.registerHandlers(handlers);
  alexa.execute();
};

 var objtwo = { "employees": [{ "account_id": 8675309 },{ "account_id": 54321 }] } // mythis variable is defined globally var mythis = this; function getJSONArray() { //var reqresponse = this.responseText; //var objtwo = JSON.parse(reqresponse); var empinfoData = objtwo.employees; var allIds = []; for (var i of empinfoData) { var employeeInfo = i["account_id"]; allIds.push(employeeInfo); }; return allIds; }; console.log(getJSONArray());

将 emploeeInfo 保存到一个数组中,并在循环后发出该数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM