Node.js处理请求体参数时报Invalid shorthand property initializer错误如何解决
报错原因
你在创建JavaScript对象时错误使用了等于号=为对象属性赋值,JavaScript语法规定定义对象的键值对必须使用冒号:分隔键和值,这一基础语法错误直接触发了Invalid shorthand property initializer(无效的简写属性初始化器)的语法报错。
修复方案
方案1:直接替换符号
将对象内所有赋值用的等号=替换为冒号:即可,修复后代码如下:
module.exports.pdf = async (event, context, callBack) => { const data = { Customer_Full_Name: event.body.Customer_Full_Name, Customer_adress: event.body.Customer_adress, customerID_number: event.body.customerID_number, CurrentDate: event.body.CurrentDate, Digital_Signature: event.body.Digital_Signature } // 此处补充你的后续业务逻辑 }
方案2:使用对象解构简化代码
由于你需要提取的属性名和最终生成的data对象的属性名完全一致,可以用对象解构写法简化代码,可读性更高:
module.exports.pdf = async (event, context, callBack) => { // 直接从event.body中解构需要的所有字段 const { Customer_Full_Name, Customer_adress, customerID_number, CurrentDate, Digital_Signature } = event.body // 用对象简写语法生成data对象 const data = { Customer_Full_Name, Customer_adress, customerID_number, CurrentDate, Digital_Signature } // 此处补充你的后续业务逻辑 }
内容的提问来源于stack exchange,提问作者Bunny
相关产品推荐
相关产品推荐

