getDeviceList() hangs indefinitely

Hi, I am trying to use the getDeviceList snippet from here, in the plugin example code but that method never appears to return.

  1. const { core, ServiceModule } = require(“@tago-io/tcore-sdk”);

  2. const express = require(‘express’);

  3. const app = express();

  4. let server = null;

  5. const service = new ServiceModule({

  6.  id: "getting-started-service",
    
  7.  name: "Getting Started Service",
    
  8.  configs: \[
    
  9.      {
    
  10.          type: "number",
    
  11.          field: "port",
    
  12.          name: "Port",
    
  13.          required: true,
    
  14.      }
    
  15.  \],
    
  16. });

  17. service.onLoad = async (userValues) => {

  18.  if (!userValues.port) {
    
  19.      throw new Error("Invalid port");
    
  20.  }
    
  21.  app.get('/', (req, res) => {
    
  22.      res.send('Hello World')
    
  23.  });
    
  24.  server = app.listen(userValues.port);
    
  25.  getDeviceList();
    
  26. };

  27. async function getDeviceList() {

  28.  try {
    
  29.      console.log("Before getDeviceList()");
    
  30.      const list = await core.getDeviceList();
    
  31.      console.log("Retrieved", list.length, "devices"); 
    
  32.  } catch {
    
  33.      (error) => { console.log(error) };
    
  34.  }
    
  35.  service.onDestroy = () => {
    
  36.      server.close();
    
  37.  };
    
  38. }

I see the “Before getDeviceList()” log message but never the “Retreived” message.

I did add the Device permission in the manifest.

Am I missing something?

Cheers,

Erwin

Hi Erwin,

It seems that the catch block in your code has a syntax error, which is preventing the error message from being logged to the console. Furthermore there is a bug in the getDeviceList method that makes it not work without parameters, this will be fixed in the future.

Here’s an example of your code fixed:

const { core, ServiceModule } = require("@tago-io/tcore-sdk");const express = require('express');const app = express();let server = null;const service = new ServiceModule({id: "getting-started-service",name: "Getting Started Service",configs: [{type: "number",field: "port",name: "Port",required: true,defaultValue: 3333,}],});service.onLoad = async (userValues) => {if (!userValues.port) {throw new Error("Invalid port");}app.get('/', async (req, res) => {return res.send(await getDeviceList());});server = app.listen(userValues.port);};async function getDeviceList() {try {console.log("Before getDeviceList()");const list = await core.getDeviceList({ page: 1, amount: 10 });console.log("Retrieved", list.length, "devices");return list;} catch(error) {console.log(error);return { error }}}service.onDestroy = () => {server.close();};