Alexa的拉姆达JS如何访问外部.js文件阵列数据

问题描述 投票:0回答:2

我建立在拉姆达与JS代码的Alexa的技能。我有2个文件。一个具有用于Alexa的技术人员的工作常规代码,以及第二文件具有与我将需要触发Inttent时数据的数组。

¿我有什么代码才能使用,让alexa排名从第二个文件中读取数组数据?

文件1

let CountryInfoSlot = resolveCanonical(this.event.request.intent.slots.CountryInfo);

console.log (CountryInfoSlot);

CountryInfoSlot = CountryInfoSlot.toLowerCase();

if (CountryInfoSlot == 'France'){

var FranceInfo = require ('/FranceInfo.js');

var N = FranceInfo.length;

var index = Math.round(Math.random()*(N-1));

var answer = FranceInfo[index];

this.response.speak(answer);

this.emit(':responseReady);

}

文件2

var FranceInfo = [

'The language spoken in France is french',

'Paris is the capital of France',

];
javascript alexa-skills-kit
2个回答
0
投票

你必须改变你的信息文件导出数据。改变你的文件2导出您要访问这样的变量:

var FranceInfo = [

'The language spoken in France is french',

'Paris is the capital of France',

];

module.exports.data = FranceInfo;

然后你可以要求变量像这样第一个文件:

const FranceInfoData = require('./FranceInfo');

var FranceInfo = FranceInfoData.data;

然后你FranceInfo变量就等于在外部文件中的数组。

这不是要做到这一点的唯一方法,但它是最简单的一种。


0
投票

您既可以读取使用fs和值存储在第二个文件的数组,并在第一解析。

要么

因为你在第一个文件使用require,在第二个文件的末尾使用module.exports = FranceInfo,以便它可以被加载到第一

© www.soinside.com 2019 - 2024. All rights reserved.