我目前正在nodejs中创建一个Dialogflow聊天机器人,并且在部署我的代码时收到一条错误消息。我试图取消对大多数事情的评论,只剩下基本的功能代码,但我仍然无法使它正常工作。我不确定这是什么问题
'use strict';
import {getAPIresponse} from "./api/index.js";
// const http = require('https');
// const respond = fulfillmentText => {
// return {
// statusCode: 200,
// body: JSON.stringify({
// fulfillmentText
// }),
// headers: {
// "Content-Type": "application/json"
// }
// }
//
// };
module.exports.dining = async (event,context) => {
const incoming= JSON.parse(event.body).queryResult;
console.log(`INCOMING: ${incoming.parameters.hall}`);
const {
displayName
} = incoming.intent;
console.log(displayName);
//const menu = getAPIresponse('https://esb.prod.uds.harvard.edu/api/dining/2.0/','events?locationId=36');
//console.log(menu);
// if(displayName === 'dining'){
// if(incoming.parameters.meal === 'breakfast'){
// //get's dining hall code to include in API request
// const hall = getCode(incoming.parameters.hall);
// //generate response from API based off of parameters passed by user
// const menu = getAPIresponse("https://esb.prod.uds.harvard.edu/api/dining/2.0/","events?locationId=${hall}", hall);
// console.log(menu);
// }
// if(incoming.parameters.meal === 'lunch'){
// //get's dining hall code to include in API request
// const hall = getCode(incoming.parameters.hall);
// //generate response from API based off of parameters passed by user
// const menu = getAPIresponse("https://esb.prod.uds.harvard.edu/api/dining/2.0","/events", hall);
// }
// if(incoming.parameters.meal === 'dinner'){
// //get's dining hall code to include in API request
// const hall = getCode(incoming.parameters.hall);
// //generate response from API based off of parameters passed by user
// const menu = getAPIresponse("https://esb.prod.uds.harvard.edu/api/dining/2.0","/events", hall);
// }
// }
};
几乎所有内容都已被注释掉,但我仍然收到读取错误消息
2019-07-02 16:31:33.351 (-04:00) undefined ERROR Uncaught Exception {
"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Unexpected tok
en {","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token {"," at
_loadUserApp (/var/runtime/UserFunction.js:98:13)"," at Object.module.exports.loa
d (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/ind
ex.js:36:30)"," at Module._compile (internal/modules/cjs/loader.js:701:30)"," a
t Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)"," at Modu
le.load (internal/modules/cjs/loader.js:600:32)"," at tryModuleLoad (internal/modu
les/cjs/loader.js:539:12)"," at Function.Module._load (internal/modules/cjs/loader
.js:531:3)"," at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)",
" at startup (internal/bootstrap/node.js:283:19)"]}
同样,我不能完全确定这里发生了什么,我会提供getAPIresponse代码,但是我的handler.js的任何地方都没有使用它,因此可以排除。
答案 0 :(得分:5)
AWS Lambda不支持您在此处编写的ES6 import
说明符
import {getAPIresponse} from "./api/index.js";
因为默认情况下Node.js不支持ES6 import
语法(注意:我的lambda运行时设置为Node.js 10.x)。
插图:
在lambda发行版的index.js
文件顶部导入库时,我也遇到了这个问题。
当我使用Uncaught Exception { "errorType":"Runtime.UserCodeSyntaxError", ... unexpected token import found ... blabla... } ...
语法时,在我的lambda函数中引发了堆栈跟踪import
:
import awsServerlessExpress from 'aws-serverless-express';
exports.handler = (event, context) => {
console.log('hello world!')
};
但是当我只是使用标准模块require
语法时,下面的版本中没有此内容。
const awsServerlessExpress = require('aws-serverless-express');
exports.handler = (event, context) => {
console.log('hello world!')
};
对我来说,是import
语法引起了SyntaxError
异常,但请注意,对您来说,当前Node.js运行时不支持的任何JavaScript语法都会抛出该异常。
一些解决方案:
将所有import
语句更改为标准模块require
语句,并继续使用已配置的Node.js运行时支持的任何默认JavaScript版本。
在将其部署到云之前,使用Babel w / Webpack之类的编译器来翻译ES6 JavaScript。
答案 1 :(得分:5)