Help needed on a custom function code

hello guys. I’m new to javascript so thanks for your patience. I have the following js code that i want to write in a custom function in n8n.

const ewelink = require('ewelink-api');
const connection = new ewelink({
              email: '[email protected]',
              password: '****************',
              region: '**',
(async () => {
const device = await connection.getDevice('1111111111');

this function returns the following json object if executed from a shell with console.log output

  _id: 'sdfsdfsdfsdfsdfsdf',
  family: { id: 'sdfsdfsdfsdfsdf', index: 0 },
  group: '',
  online: true,
  groups: [],
  devGroups: [],
  relational: [],
  deviceid: '111111111',
  name: 'test',
  type: '10',
  apikey: 'sdfsdfsdfsdfsdfsdfsdf',
   { _id: 'sdfsdfsdfsdfsdf',
     apikey: 'sdfsdfsdfsdfsdf',
      { uiid: '15',
        description: 'WO1882004',
        brandId: 'sdfsdfsdfsdf',
        apmac: 'zzzzzzzzzzzz',
        mac: 'zzzzzzzzzzzzzzzzzzzz',
        ui: 'sdfsdfsdfsdfsdf',
        modelInfo: 'zzzzzzzzzzzzzzzzzz',
        model: 'rghtghfdghfgh',
        manufacturer: 'wdfsdfsdfsdf',
        staMac: '123123123123',
        chipid: 'ergergerg' },
     country: 'cv',
     expiredAt: '2023-11-19T00:00:00.000Z',
     deviceid: '1111111111',
     createdAt: '2018-11-20T07:27:42.501Z' },
   { opsNotify: 0,
     opsHistory: 1,
     alarmNotify: 1,
     wxAlarmNotify: 0,
     wxOpsNotify: 0,
     wxDoorbellNotify: 0,
     appDoorbellNotify: 1 },
  createdAt: '2021-01-02T08:55:37.643Z',
   [ { family: [Object],
       apikey: 'yjtyjtyjtyjtyjtyjty',
       permit: 15,
       shareDate: '2021-01-02T08:56:28.981Z',
       comment: '' } ],
  __v: 0,
  onlineTime: '2021-01-16T16:24:35.028Z',
   { version: 8,
     sledOnline: 'on',
     init: 1,
     switch: 'off',
     fwVersion: '3.4.1',
     rssi: -74,
     staMac: 'sdfsdfsdf',
     startup: 'stay',
     pulse: 'off',
     pulseWidth: 500,
     deviceType: 'temperature',
     sensorType: 'AM2301',
     currentHumidity: '50',
     currentTemperature: '38.3',
     mainSwitch: 'on',
     targets: [ [Object], [Object] ] },
  ip: '',
  location: '',
  offlineTime: '2021-01-16T16:24:22.297Z',
   { clientInfo:
      { model: 'Redmi Note 5_sdm636',
        os: 'Android',
        imei: 'werwewfewe',
        romVersion: '9',
        appVersion: '4.8.0' },
     _id: 'dfgdfgdfgdfg',
     email: '[email protected]',
     appId: 'dfgdfgdfgdfgdfgdfg',
     countryCode: '+44',
     accountInfo: { level: 10 },
     apikey: 'dfgdfgdfgdfgdfgdfgdfg',
     createdAt: '2021-01-02T08:45:43.237Z',
     __v: 0,
     language: 'en',
     lang: 'en',
     extraPush: { dfgdfgdfgdfgdfgdfgdfgdfg: [Object] },
      { lastEmailAdAt: '2021-01-02T08:45:45.617Z', ipCountry: 'FF' },
     isAccepEmailAd: true,
     currentFamilyId: 'DFGDFGDFGDFGDFG',
     appInfos: [ [Object] ],
     online: false,
     onlineTime: '2021-01-16T17:05:04.245Z',
     ip: '',
     location: '',
     offlineTime: '2021-01-16T17:05:19.317Z' },
  brandName: 'Sonoff',
  productModel: 'TH16',
  uiid: '15'

my need is to be able to execute it with n8n function node, and then parse the data and do other stuff in the workflow. however until now, i’m stuck with the following error: ERROR: Always an Array of items has to be returned! and im not sure how to resolve it as im not a js developer.

any help is highly appreciated.

The following should work:

const ewelink = require('ewelink-api');
const connection = new ewelink({
              email: '[email protected]',
              password: '****************',
              region: '**',
const device = await connection.getDevice('1111111111');

return [
    json: device

Thanks @jan , it looks better now.
I have 2 more questions please:
1- can I add an async method to the call as such? whenever I add it, I got the same error again.

(async () => {
const device = await connection.getDevice('1111111111');
return [
    json: device

2- the output of the function node needs to go as argument to an IF node. how can I make my condition there? I tried the following: value1: {{$node[“Function”]}} condition: equal: value2: true
so I need to know if my device is online or not.

  1. The question is more why do you want to? Is there a reason for that, esp. because you say you are new to JavaScript. Because sure we can make it work with an additional async, we could even add 10 but all it would do is to make the code totally unnecessarily more complicated. So possible => “Yes”, should you do it => “probably not, unless there is a good reason”. Anyway if you really want to have an async in there for what reason ever, adding a return in the first line should make it work. So return (async () => {....
  2. If the Function-Node is called “Function” and you do set it as boolean on the If-Node it should work. You could also use the shorter version {{ $ }}

Thanks @jan, everything is working fine now.
I’m not sure why I need the async method, I’m just following the following documentation I’m not sure if it is needed or not.
please let me know if it is not mendatory

Ah, no in this case it is not needed. You normally need “async” to be able to use “await” but because the n8n Function-Node wraps everything automatically in an async-Function you will not need here.

1 Like