You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Lambda出现Syntax Error: missing ) after argument list语法错误求助

语法错误排查与解决方案

错误原因

直接触发missing ) after argument list语法报错的核心原因是多层括号/花括号未正确闭合,同时代码还存在多个不符合Lambda运行规范的问题:

  • exports.handler入参缺失context、callback,但代码内部调用了这两个Lambda内置对象
  • then()回调内传递给callback的响应对象未闭合},then()回调本身未闭合}),handler函数整体也未闭合},多层括号不匹配直接触发语法错误
  • 非致命业务错误:HTML标签<hl>应为<h1>,字符集拼写错误uft-8应为utf-8,Lambda响应的头部字段应为headers而非header,async handler与callback混用不符合Node.js Lambda最佳实践

修复后完整可运行代码

const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB.DocumentClient({region: 'ap-southeast-1'})

// 补全Lambda handler标准入参
exports.handler = async(event, context, callback) => {
    var obj = JSON.parse(JSON.stringify(event.queryStringParameters));
    const requestId = context.awsRequestId;
    await createMessage(requestId).then(() => {
        callback(null, {
            statusCode: 200, 
            body:  `
                <h1>query string parameters</h1>
                <p>${JSON.stringify(event.queryStringParameters)}</p>
                <p>one: ${obj.one}</p>
                <hr/>
                <h1>path</h1>
                <p>${JSON.stringify(event.requestContext.http.path)}</p>
                <hr/>
             `, 
           headers: {
                'Content-Type' :  'text/html; charset=utf-8',
            },
        }) // 闭合callback响应对象
    }) // 闭合then回调函数
}; // 闭合handler函数

function createMessage(requestId) {
     const params = {
        TableName: 'Message',
        Item: {
            'Hello' : 'one'
        }
     }
    return ddb.put(params).promise();
};

最佳实践优化方案(可选)

async类型的Lambda handler无需使用callback,直接return响应对象即可,代码逻辑更简洁不易出错:

const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB.DocumentClient({region: 'ap-southeast-1'})

exports.handler = async(event, context) => {
    var obj = JSON.parse(JSON.stringify(event.queryStringParameters));
    const requestId = context.awsRequestId;
    await createMessage(requestId);
    return {
        statusCode: 200, 
        body:  `
            <h1>query string parameters</h1>
            <p>${JSON.stringify(event.queryStringParameters)}</p>
            <p>one: ${obj.one}</p>
            <hr/>
            <h1>path</h1>
            <p>${JSON.stringify(event.requestContext.http.path)}</p>
            <hr/>
         `, 
       headers: {
            'Content-Type' :  'text/html; charset=utf-8',
        },
    }
};

function createMessage(requestId) {
     const params = {
        TableName: 'Message',
        Item: {
            'Hello' : 'one'
        }
     }
    return ddb.put(params).promise();
};

内容的提问来源于stack exchange,提问作者Metpumin Konta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 20:36:09