Not getting msg from function node

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) = JSON.stringify(deviceInfo);

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

return msg; //nothing is returned


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:

Thank you, that was the issue.