开发者社区> 问答> 正文

JSON.parse()导致没有任何数据的错误

我正在写一本js AWS Lambda Function 。

我会请求API而不js event.body 进
行测试

export const check = async (event) => {
  try{
    console.log("it does work");
    const {email, uid} = JSON.parse(event.body); // email and uid both would be undefined..
    //JSON.parse() throw some Error, so the below codes doesn't work.
    //Also, I can't see any single word from the response.
    if(!email && !uid) throw "No body data!"; // I expected throw this string but it didn't happend
    return {
      statusCode: 200,
      body: JSON.stringify({msg: "SUCCESS"})
    }
  }catch(e){
    return {
      statusCode: 500,
      body: JSON.stringify({e: e})
    }
  }
}

我可以得到这个回应。

{
  e: {}
}

我想看看

{
  e: "Some sentences about Error caused by JSON.parse() or No body data!"  
}

展开
收起
几许相思几点泪 2019-12-03 16:21:11 462 0
0 条回答
写回答
取消 提交回答
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载