【问题标题】:Whys can't I access httprequest variable values?为什么我不能访问 httprequest 变量值?
【发布时间】:2021-04-14 10:19:31
【问题描述】:

我正在使用 httprequest 的主体,如下所示:

const httpTrigger: AzureFunction = async function (
  context: Context,
  req: HttpRequest
): Promise<void> {
  try {

     for (var key in req.body) {
      console.log('req.body v name=' + key + ' value=' + req.body[key]);
     }
     const rbi=req.body?.iID;
     console.log({rbi});
     const rbi2=req.body['iID'];
     console.log({rbi2});

第一个显示给出:

req.body v name=aT value=uuid_value
req.body v name=iID value=some_text

第二个和第三个:

{ rbi: undefined }
{ rbi2: undefined }

但我认为他们应该是some_text。我错过了什么?

【问题讨论】:

  • 你在做console.log(JSON.stringify(req.body));时看到了什么
  • uuid_value 和 some_text 的变量。
  • 将 json 作为文本发布
  • {"aT":"uuid_value","iID":"some_text"}
  • 当你登录console.log(rbi2);时你看到了什么

标签: node.js azure httprequest


【解决方案1】:

您的代码编译不正确:

此外,当我删除以下行时:

const rbi=req.body?.iID;
     console.log({rbi});

代码有效,你得到some_text而不是undefined

查看https://onecompiler.com/nodejs/3wuknh7g3

这是 Function App 中的相同代码:

【讨论】:

  • 嗨。谢谢,但你错过了这个:const httpTrigger: AzureFunction = async function ( context: Context, req: HttpRequest
  • 你的意思是在 azure 函数中你看到了这个问题?
  • PLs 检查我更新的回复。同样的事情在 Function App 中也可以正常工作。
  • 您正在创建一个新变量 body。我的是请求的正文。
  • 是一样的...请检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-14
  • 2019-05-04
相关资源
最近更新 更多