无法解析 Azure 函数中的 json 消息正文

Unable to parse json message body in Azure functions

我的 Azure Function

开头有以下 .js 代码
module.exports = async function (context, messageObj) {

    context.log('Incoming Message:  ', messageObj)
    const {
        user_name
    } = messageObj

    context.log('typeof messageObj:  ', typeof messageObj)
    context.log('user_name:  ', user_name)

但是,user_name 仍未定义。

typeof messageObj:   object    
user_name:   undefined

function.json:

    {
  "bindings": [
    {
      "name": "messageObj",
      "type": "httpTrigger",
      "direction": "in",
    }
  ]
}

我的问题是为什么 user_name 未定义且未被解析,我缺少什么?

context.log('Incoming Message: ', messageObj):

的输出
Incoming Message:   {
  method: 'GET',
  url: 'http://localhost:7071/api/HttpExample',
  originalUrl: 'http://localhost:7071/api/HttpExample',
  headers: {
    accept: '*/*',
    connection: 'keep-alive',
    host: 'localhost:7071',
    'user-agent': 'PostmanRuntime/7.28.4',
    'accept-encoding': 'gzip, deflate, br',
    'content-type': 'text/plain',
    'content-length': '5994',
    'postman-token': '1f1e66c3-19b5-400a-94fb-a91283a53346'
  },
  query: {},
  params: {},
  body: {
    ...
    user_name: 'some user',
    used_subscription: false,
    ...
}

messageObj 本身没有任何密钥 user_name。您正在尝试对其进行解构,但它不存在,因此它将 return undefined(就像您刚刚尝试直接访问它一样,la messageObj.user_name)。

根据您提供的信息,实际上 body:

const { user_name } = messageObj.body;

在一个watered-down例子中:

const messageObj = {
  foo: 'bar',
  bar: 'foo',
  user: {
    user_name: 'the_user_name',
    foobar: 'barfoo'
  }
}

const { user_name } = messageObj.user;

console.log(user_name);