【问题标题】:Using the Alexa-SDK how can I get this.emit() method to return multiple objects in a JSON array?使用 Alexa-SDK 如何让 this.emit() 方法返回 JSON 数组中的多个对象?
【发布时间】:2017-10-24 17:44:51
【问题描述】:

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

这是我的代码 sn-p:

'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();
};

【问题讨论】:

    标签: javascript arrays json node.js function


    【解决方案1】:

    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 保存到一个数组中,并在循环后发出该数组。

    【讨论】:

    • 关于为什么投反对票的任何信息?我没有完整的代码示例可供使用,所以尽我所能。
    • 您回答了不完整的问题。您没有要求 emit 方法定义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多