java script error“Uncaught SyntaxError:Unexpected token =>”

时间:2018-03-07 01:48:23

标签: javascript

这是我的代码,当我在lambda中运行它时。它运行正常,但是当我在我的本地服务器上运行它时会出错[第22行:未捕获的SyntaxError:意外的令牌=>]

这是我的第一个JS。 :)

有人可以帮我理解这个错误。

谢谢!

"use strict";
const https = require('https');
const querystring = require('querystring');

const data = querystring.stringify({
    'input' :  'test is passed'
});

const options = {
    hostname: 'abc.xyz',
    port: 443,
    path: '/DEV/-testresult',
    method: 'POST',
    headers: {
        'Content-Type' : 'application/json',
        'x-api-key' : 'xxxxxx',
        'X-Amz-Invocation-Type' : 'Event'
    }

};

const req = https.request(options, (res) => {
    let body = '';
    console.log('Status:', res.statusCode);
    console.log('Headers:', JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', (chunk) => body += chunk);
    res.on('end', () => {
        console.log('Successfully processed HTTPS response');
        // If we know it's JSON, parse it
        if (res.headers['content-type'] === 'application/json') {
            body = JSON.parse(body);
        }
    });
});

req.on('error');
req.write(JSON.stringify(data));
req.end();

1 个答案:

答案 0 :(得分:0)

在箭头功能上使用功能代替。它会工作。

    const req = https.request(options, **function**(res){
    let body = '';
    console.log('Status:', res.statusCode);
    console.log('Headers:', JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', (chunk) => body += chunk);
    res.on('end', () => {
        console.log('Successfully processed HTTPS response');
        // If we know it's JSON, parse it
        if (res.headers['content-type'] === 'application/json') {
            body = JSON.parse(body);
        }
    });
});