Search code examples
node.jsazureazure-sql-databasedashboardazure-iot-hub

ArgumentError('The object \'deviceInfo' is missing the property: deviceId'); How do I solve this?


I am working with Azure IoT Hub and trying to retrieve real devices (not simulated) which are connected to our workplace Hub. The project is that we're creating a company dashboard to display and edit various aspects of devices from our IoT Hub. I am an intern, so this is truly my first time ever doing ANYTHING with IoT Hub, so please forgive naivety. I am testing just to send basic information about devices in a "registry.create" function, but I keep receiving aforementioned error. Also, our database is Azure SQL.

Here is my code for the section. Only necessary code is posted:

var iothub = require('azure-iothub');
var connectionString = '<Our connection string here>';
var registry = iothub.Registry.fromConnectionString(connectionString);

...

const device = {
    deviceId: registry.deviceId,
    devStatus: registry.status,
    connStatus: registry.connectionState
}

registry.create(device, function(err, deviceInfo, res) {
    if (err) {
        registry.get(device.deviceId, printDeviceInfo);
        console.log(device.deviceId);
    }
    if (deviceInfo.deviceId) {
        printDeviceInfo(err, deviceInfo.deviceId, res)
    }
});

function printDeviceInfo(err, deviceInfo, res) {
    if (deviceInfo) {
        console.log('Device ID: ' + deviceInfo.deviceId);
        console.log('Connection state: ' + deviceInfo.connStatus)
    }
}

Here is database model for devices:

module.exports = (sequelize, Sequelize) => {
    const Device = sequelize.define('devices', {
        deviceId: {
            type: Sequelize.STRING,
            required: true
        },
        devStatus: {
            type: Sequelize.BOOLEAN,
            allowNull: false,
            defaultValue: true
        },
    });
    return Device;
}

Not sure how much assistance can be suggested, but anything is appreciated. Not much is online about this error (literally two pages in Google and neither were similar to my specific problem)..


Solution

  • If you see the source code of IOT hub SDKs, you will find that the create method shall throw ArgumentError if the first argument does not contain a deviceId property:

    enter image description here

    So, your deviceId property is not populated. create(DeviceDescription, HttpResponseCallback) creates a new device identity on an IoT hub. You will have to provide a value to it. Registry does not provide those properties you have used in your code. Refer this sample for proper usage of registry.create. And, I would recommend you to read Understand the identity registry in your IoT hub.

    Let me know if you have further questions.