当我的代码中没有语法错误时,为什么会出现UserCodeSyntaxError? [英] Why do I get a UserCodeSyntaxError when I have no syntax error in my code?
问题描述
我目前正在nodejs中创建一个Dialogflow聊天机器人,并且在部署我的代码时收到一条错误消息.我试图取消注释大多数事情,只剩下基本的功能代码,但我仍然无法使它正常工作.我不确定这是什么问题
I'm currently creating a Dialogflow chatbot in nodejs and upon deploying my code I get an error message. I've attempted to uncomment most things out to just be left with the base functioning code and I am still unable to get it working. I'm not exactly sure what the issue is here
'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);
// }
// }
};
几乎所有内容都已被注释掉,但我仍然收到显示为
Almost everything is commented out and I still get the error message that reads
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)"]}
推荐答案
AWS Lambda不支持您在此处编写的ES6 import
说明符
AWS Lambda does not support the ES6 import
specifier as you've written here
import {getAPIresponse} from "./api/index.js";
因为Node.js默认不支持ES6 import
语法(注意:我的lambda运行时设置为Node.js 10.x).
because the ES6 import
syntax isn't yet supported by default in Node.js (note: my lambda runtime was set to Node.js 10.x).
插图:
在我的lambda发行版的index.js
文件顶部导入库时,我也遇到了这个问题.
I was having this issue as well when importing a library at the top of my lambda distribution's index.js
file.
当我使用import
语法时,在我的lambda函数中引发了堆栈跟踪Uncaught Exception { "errorType":"Runtime.UserCodeSyntaxError", ... unexpected token import found ... blabla... } ...
:
The stacktrace Uncaught Exception { "errorType":"Runtime.UserCodeSyntaxError", ... unexpected token import found ... blabla... } ...
was thrown in my lambda function when I used the import
syntax:
import awsServerlessExpress from 'aws-serverless-express';
exports.handler = (event, context) => {
console.log('hello world!')
};
但是当我只是使用标准模块require
语法时,在下面的此版本中却没有.
But not in this version below when I just used the standard module require
syntax.
const awsServerlessExpress = require('aws-serverless-express');
exports.handler = (event, context) => {
console.log('hello world!')
};
对我来说,是导致SyntaxError
异常的原因是import
语法,但请注意,对您来说,当前Node.js运行时不支持的任何JavaScript语法都将引发此异常.
For me, it was the import
syntax that was causing the SyntaxError
exceptions, but do take note that, for you, any JavaScript syntax not supported by your current Node.js runtime will throw this exception.
一些解决方案:
-
将所有
import
语句更改为标准模块require
语句,并继续使用配置的Node.js运行时支持的任何默认JavaScript版本.
Change all
import
statements to standard modulerequire
statements and keep using whatever default JavaScript flavour is supported by your configured Node.js runtime.
在部署到云之前,使用Babel w/Webpack之类的编译器来翻译ES6 JavaScript.
Use a transpiler like Babel w/ Webpack to transpile your ES6 JavaScript before deploying to the cloud.
这篇关于当我的代码中没有语法错误时,为什么会出现UserCodeSyntaxError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!