PelionIoT / mbed-cloud-sdk-javascript

Pelion Device Management SDK for JavaScript
Apache License 2.0
9 stars 10 forks source link

ConnectAPI throws error when connecting device to ARM Pelion #593

Open Antondj5 opened 4 years ago

Antondj5 commented 4 years ago

While trying to create a connector to subscribe to value changes of devices on the ARM Pelion platform I ran in to an issue. When connecting my device (a linux vm) to the Pelion platform (using this tutorial) while the following code is running I get the error you can see below.

const sdk = require('mbed-cloud-sdk')

const connect = new sdk.ConnectApi({
  apiKey: 'apikey',
  host: 'https://api.us-east-1.mbedcloud.com',
  autostartNotifications: true
})

const main = async () => {

  try {
    let observer = await connect.subscribe.resourceValues({ resourcePaths: ['*'] }, 'OnRegistration')
    observer.addListener((message) => {
      console.log(message)
    })
  } catch (e) {
    throw e
  }

}

main()
/arm-connector/node_modules/mbed-cloud-sdk/lib/legacy/connect/models/deviceEventAdapter.js:30
             type: common_1.TlvDataType[from.ct],
                                       ^

 TypeError: Cannot read property 'undefined' of undefined
     at Function.DeviceEventAdapter.mapResource (/arm-connector/node_modules/mbed-cloud-sdk/lib/legacy/connect/models/deviceEventAdapter.js:30:39)
     at /arm-connector/node_modules/mbed-cloud-sdk/lib/legacy/connect/models/deviceEventAdapter.js:39:43
     at Array.map (<anonymous>)
     at Function.DeviceEventAdapter.map (/arm-connector/node_modules/mbed-cloud-sdk/lib/legacy/connect/models/deviceEventAdapter.js:38:40)
     at /arm-connector/node_modules/mbed-cloud-sdk/lib/legacy/connect/actions/notifications.js:114:63
     at Array.forEach (<anonymous>)
     at Object.exports.notify (/arm-connector/node_modules/mbed-cloud-sdk/lib/legacy/connect/actions/notifications.js:113:28)
     at /arm-connector/node_modules/mbed-cloud-sdk/lib/legacy/connect/actions/notifications.js:214:37
     at NotificationsApi.ApiBase.complete (/arm-connector/node_modules/mbed-cloud-sdk/lib/legacy/common/apiBase.js:262:13)
     at /arm-connector/node_modules/mbed-cloud-sdk/lib/legacy/common/apiBase.js:219:19
 Waiting for the debugger to disconnect...
 error Command failed with exit code 1.

Am I doing something wrong or is this a problem with the SDK?

The code works fine when I start my application and my device is already registered. The code fails when the code is running and I connect the device afterwards (execute the script -> see link to tutorial above).

Antondj5 commented 4 years ago

When commenting out type: common_1.TlvDataType[from.ct], in /arm-connector/node_modules/mbed-cloud-sdk/lib/legacy/connect/models/deviceEventAdapter.js:30 my code works as expected.

 DeviceEventAdapter.mapResource = function (from, deviceId) {
        console.log('from', from)
        console.log('common_1', common_1)
        return {
            contentType: from.rt,
            observable: from.obs,
         //   type: common_1.TlvDataType[from.ct],
            path: from.path,
            deviceId: deviceId,
        };
    };
alexl0gan commented 4 years ago

What environment are you running in? I just added a test to check the mapping and it worked fine. It looks like either something has gone wrong with the module resolution or your environment can't understand the enum.

Antondj5 commented 4 years ago

I'm running a node v10.19.0 environment on MacOS Mojave v10.14.6. Do I need a more recent version of node? Edit: I also use yarn v1.22.0 as a package manager.

alexl0gan commented 4 years ago

I use 10.18.0 so that shouldn't be an issue. I will investigate further today