Unable to use return value from custom module in later action

I am building a custom module in NodeJS to load a key value from a config file. The module successfully reads and returns the config setting; however, if this is used later, it is always undefined.

hjson file

{
    type: 'settings',
    module: 'settings',
    groupTitle: 'Settings',
    groupIcon: 'fas fa-lg fa-database comp-data',
    action: 'loadFromKey',
    title: 'Load From Key',
    icon: 'fas fa-lg fa-database comp-data',
    dataScheme: [
        { name: 'Value', optionName: 'value', title: 'Value', initValue: true, defaultValue: false, type: 'text'}
    ],
    dataPickObject: true,
    globalVars: {},
    properties: [
        {
            variables: [
                {
                    name: 'name',
                    optionName: 'name',
                    title: 'Name',
                    type: 'text',
                    required: false,
                    defaultValue: 'name'
                }
                ,{
                    name: 'output',
                    optionName: 'output',
                    title: 'Output',
                    type: 'boolean',
                    defaultValue: false
                }
                ,{
                    name: 'actionKey',
                    optionName: 'key',
                    title: 'Key',
                    type: 'text',
                    required: true,
                    defaultValue: '',
                    serverDataBindings: true,
                    help: 'The config key.'
                }
            ]
        }
    ]
}

js file

const settings = require('../../../settings.json')

exports.loadFromKey = function(options, name) {
    let configKey = this.parse(options.key, 'string', 'The config key is required.');
    console.log(settings[configKey]);
    return settings[configKey];
}

Custom module use
image

1 Like

Ok, not near computer but from memory you need to return the value as json

Try

return { "configKey" : settings[configKey] }
2 Likes

Thank you,

Returning as Json worked:

const settings = require('../../../settings.json')

exports.loadFromKey = function(options, name) {
    let configKey = this.parse(options.key, 'string', 'The config key is required.');
    console.log(settings[configKey]);
    return { "Value": settings[configKey] };
}
1 Like

Hello @Hyperbytes,

I m trying to do a simple custom module and I saw this thread.
I go the same problem but cannot figure out how to solve it.
In the webserver log I got all the result of the query but nothing happens in the response which is always empty.

Can you give a look to my code please (I m a newbie on this so please have mercy)

search_via.js

const mysql = require('mysql2/promise');  // Aggiungi questa riga per importare mysql2/promise

exports.searchViaById = async function(options) {
    const id = options.id;  // Ottiene il parametro ID
    
    // Configurazione del database
    const dbConfig = {
        ......
        }
    };

    try {
        // Connessione al database
        const connection = await mysql.createConnection(dbConfig);
        
        // Esegue la query per cercare la via per ID
        const [rows] = await connection.execute('SELECT * FROM vie_citta WHERE id = ?', [id]);
        
        await connection.end();
        
        // Se troviamo un record, lo ritorniamo con una chiave esplicita
        if (rows.length > 0) {
            return { "result": rows[0] };  // Restituiamo i risultati come "result"
        } else {
            return { message: 'No record found' };  // Nessun record trovato
        }
    } catch (error) {
        console.error(error);
        return { message: 'Error occurred while querying the database' };
    }
};

the hjson file:

{
  type: 'search_via_by_id',
  module: 'search_via',
  action: 'searchViaById',
  groupTitle: 'Custom Actions',
  groupIcon: 'fas fa-lg fa-database comp-data',
  title: 'Search Via by ID',
  icon: 'fas fa-lg fa-search',
  dataScheme: [
    { name: 'id', type: 'number' },
    { name: 'nome_via', type: 'text', output: true },  // Abilita l'output di 'nome_via'
    { name: 'codice_postale', type: 'text', output: true },  // Abilita l'output di 'codice_postale'
    { name: 'comune', type: 'text', output: true },  // Abilita l'output di 'comune'
    { name: 'data_inserimento', type: 'datetime', output: true }  // Abilita l'output di 'data_inserimento'
  ],
  dataPickObject: true,  // Questo rende i dati selezionabili nel Dynamic Data Picker
  properties: [
    {
      group: 'Via Search Properties',
      variables: [
        { name: 'id', optionName: 'id', title: 'ID', type: 'number', required: true }
      ]
    }
  ]
}

Thank you

Roberto

The issue may be due to an hjson issue
Firstly you name using the actionName option to pass the id. while this is, i suppose, possible, not normal. Pass the option name and id separately.

Lastly, but most significantly, you are missing the output option so no output will occur

      variables: [
{ name: 'actionName', optionName: 'name', title: 'Name',   type: 'text', required: true, defaultValue: '', baseName: "myquery1"},
{ name: 'id', optionName: 'id', title: 'Record ID',   type: 'text', defaultValue: ''},
{ name: 'output', optionName: 'output', title: 'Output', type: 'boolean', defaultValue: false 
  }

If you must be aware the actionName parameter is not passed in the "options" collection, it is passes separately as "name" so you would need

exports.searchViaById = async function(options, name ) {

and use the value of "name"

Try that and come back to me if you still have issues

1 Like

you may also find this post useful re connections