Not getting msg from function node

#1

I'm new to node so I'm pretty sure I'm doing something wrong. I'm trying to take the error, status, or deviceinfo based on what is returned by the create function. If I log them to the console they return fine, but when I try to add them as properties to the msg object I get nothing back.

var iothub = global.get('iothub');
var connectionString = '';
var registry = iothub.Registry.fromConnectionString(connectionString);


// Create a new device
var device = {
    deviceId: 'sample-device-'
};



registry.create(device, function(err, deviceInfo, res) {
    if (err)        msg.err  = err.toString();
    if (res)        msg.res  = res.statusMessage;
    if (deviceinfo) msg.info = JSON.stringify(deviceInfo);
});

console.log(msg); //shows undefined in the console

return msg; //nothing is returned
0 Likes

#2

Hi

you are setting the msg properties in an asynchronous callback function. That function will be called after the function has returned.

Here's how to send messages asynchronously: https://nodered.org/docs/writing-functions#sending-messages-asynchronously

0 Likes

#3

Thank you, that was the issue.

0 Likes