Custom module " return key and value "

Hello @patrick,

I created a function like this and I want to use ‘RandomNumber’ as key. However, I cannot get output as key and value, I just see the “value” as in the screenshot and I cannot use this output in another action step. Where is the problem?


  {
    type: 'custom',
    module : 'custom',
    action : 'numbergenerator',
    groupTitle : 'Custom Actions',
    groupIcon : 'fas fa-lg fa-database comp-data',
    title : 'Number Generator',
    icon : 'fas fa-lg fa-table comp-data',
    dataScheme: [
      {name: 'RandomNumber', type: 'number'}
    ],
    dataPickObject: true,
    globalVars: {
      '$_GET' : ['RandomNumber']
    },
    properties : [
      {
        group: 'Properties',
        variables: [
          { 
              name: 'start', 
              optionName: 'start', 
              title: 'Start', 
              type: 'text', 
              required: true, 
              serverDataBindings: true,
              defaultValue: ''
          },
          { 
              name: 'end', 
              optionName: 'end', 
              title: 'End', 
              type: 'text', 
              required: true,
              serverDataBindings: true, 
              defaultValue: ''
          }
        ]
      }
    ]
  }


Function

public function numbergenerator($options, $name)
    {
        option_require($options, 'start');
        option_require($options, 'end');
        $options = $this->app->parseObject($options);
        
        if (isset($options->start) && isset($options->end)) {
            return array('RandomNumber' => rand($options->start, $options->end));
            
        } else {
            Throw new \Exception('There was a problem generating a random number.');
        }
        
    }

Server Action
image

Output
image

You should be returning a simple JSON from here and not an array.
Something like:

$r->RandomNumber = rand($options->start, $options->end);
return json_encode($r);

returned 500 error … @sid

Can you please check again. Seems to be working fine.

Or, maybe try returning the object as is, without json_encode?

image

This works too. But it will not wrap the result in “RandomNumber” as you have defined in hjson.

yes , I think problem is different

Here a working sample of your random number generator. Place the files in the extensions/server_connect/modules folder.

randomnumber.zip (852 Bytes)

returned 500 error again @patrick

What is the exact error message that is returned? I tested the files and they work fine.

you can see

Looks like the web server is preventing the actual error being shown, so difficult to say what is wrong. Just recreate the whole action file.

1 Like

Have you considered just using something like a UUID as the random numeric key?
A simple random is much more likely to generate duplicates