I have following .js
code at the start of my Azure Function
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)
However, the user_name
stays undefined.
typeof messageObj: object
user_name: undefined
function.json:
{
"bindings": [
{
"name": "messageObj",
"type": "httpTrigger",
"direction": "in",
}
]
}
My question is why user_name
is undefined and not getting parsed, what am i missing?
Ouput of 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,
...
}
CodePudding user response:
messageObj
itself doesn't have any key user_name
. You're trying to desctructure it, but it's not there, so it'll return undefined
(the same as if you had just tried to directly access it, a la messageObj.user_name
).
It seems, based on the information you've provided, that it's actually under body
:
const { user_name } = messageObj.body;
In a watered-down example:
const messageObj = {
foo: 'bar',
bar: 'foo',
user: {
user_name: 'the_user_name',
foobar: 'barfoo'
}
}
const { user_name } = messageObj.user;
console.log(user_name);