Reputation: 819
I'm having a small problem. I parsed an message with an xml2js Parser
parser.parseString(message.toString(), function (err,result) {
//Extract the value from the data element
value = result;
console.log(result);
});
return value;
This correctly returns an XML object which looks like:
{message: { type: ['authMessage'], sender: ['username']} }
but know i want the data, meaning type = authMessage; sender = username;
How can i get that data? I'm not really sure, thanks for any help.
Upvotes: 0
Views: 121
Reputation: 17038
Once you've used parser.parseString()
, you get a plain Javascript object. How about:
var type = result.message.type[0];
var sender = result.message.sender[0];
Here is some documentation on Javascript variable types.
Upvotes: 1
Reputation: 13522
It looks like you are not well aware of "asynchronous" and "synchronous" concepts. console.log(result);
displays the result in the asynchronous callback, which is executed after return value;
. So value
is not initialized and function returns undefined
.
This code may work if parseString
does not perform asynchronous calls internally but it is an exceptional situation. Most of the code with callbacks works asynchronously. So you need to organize your code in this manner too.
Upvotes: 0