【发布时间】:2017-06-04 15:19:00
【问题描述】:
Node.js Alexa 任务问题
我目前正在通过 AWS Lambda 编写 Node.js Alexa 任务,并且一直在尝试编写一个函数,该函数从 OpenWeather API 接收信息并将其解析为名为 weather 的变量。相关代码如下:
var request = require('request');
var weather = "";
function isBadWeather(location) {
var endpoint = "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&APPID=205283d9c9211b776d3580d5de5d6338";
var body = "";
request(endpoint, function (error, response, body) {
if (!error && response.statusCode == 200) {
body = JSON.parse(body);
weather = body.weather[0].id;
}
});
}
function testWeather()
{
setTimeout(function() {
if (weather >= 200 && weather < 800)
weather = true;
else
weather = false;
console.log(weather);
generateResponse(buildSpeechletResponse(weather, true), {});
}, 500);
}
我在 Cloud9 和其他 IDE 中无数次地运行了这个 sn-p,它似乎工作得完美无缺。但是,当我将其压缩到一个包中并将其上传到 AWS Lambda 时,我收到以下错误:
{
"errorMessage": "Cannot find module '/var/task/index'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:276:25)",
"Module.require (module.js:353:17)",
"require (internal/module.js:12:17)"
]
}
我安装了 module-js、request 和许多其他应该使这段代码运行的 Node 模块,但似乎没有任何东西可以解决这个问题。这是我的目录,以防万一:
- planyr.zip
- index.js
- node_modules
- package.json
有人知道问题出在哪里吗?
【问题讨论】:
-
这是我的代码的日志输出:
START RequestId: 46c71292-debf-11e6-a013-1be2c415a9c1 Version: $LATEST Unable to import module 'index': Error at Function.Module._resolveFilename (module.js:325:15) at Function.Module._load (module.js:276:25) at Module.require (module.js:353:17) at require (internal/module.js:12:17) END RequestId: 46c71292-debf-11e6-a013-1be2c415a9c1 REPORT RequestId: 46c71292-debf-11e6-a013-1be2c415a9c1 Duration: 55.76 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 16 MB -
除了在 Mac 上使用终端进行压缩的答案外,还要确保您的代码文件名为“index.js”。我有一个更具描述性的名称,会产生错误。
-
@Art 这是我的问题。我压缩了一个 test.js,它抛出了一个未处理的错误。将其更改为 index.js 后,它工作正常。谢谢。
标签: javascript node.js amazon-web-services request aws-lambda