Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sails helpers and machine spec

I upgrade sails to the @^1.0.0 version and while I'm developing an API, I wanted to use a Service but the Sails document advice to use Helper now. And I don't realy use to work with the new way to discripe helper, build script or actions.

And all the try I have mad wasn't successful.

In the following exemple..

Here is my controller call:

    var ob = await ails.helpers.testy('sayHello');

    res.json({ob:ob});

helper

module.exports = {

friendlyName: 'Testy',


description: 'Testy something.',


inputs: {

  bla: {
    type: 'string'
  }

},


exits: {

  success: {

  }

},


fn: async function (inputs, exits) {

  console.log({blabla:inputs.bla})

  if(!inputs.bla) return exits.error(new Error('text not found'));

  var h = "Hello "+ inputs.bla;

  // All done.
  return exits.success(h);

}

};

I'm getting this error

error: A hook (`helpers`) failed to load!
error:
error: Attempted to `require('*-serv\api\helpers\testy.js')`, but an error occurred:
--
D:\*-serv\api\helpers\testy.js:28
  fn: async function (inputs, exits) {
            ^^^^^^^^
SyntaxError: Unexpected token function.......

and if I remove the "async" and the "await" form the Controller, the ob object return null and I'm having this error

WARNING: A function that was initially called over 15 seconds
ago has still not actually been executed.  Any chance the
source code is missing an "await"?

To assist you in hunting this down, here is a stack trace:
```
    at Object.signup [as auth/signup] (D:\*-serv\api\controllers\AuthController.js:106:26)
like image 346
Nastyo Avatar asked Apr 07 '18 03:04

Nastyo


1 Answers

The first guy from the comments is right.

After removing async from fn: async function (inputs, exists) {}; you need to setup sync: true which is false by default. It is described at helpers doc page at Synchronous helpers section.

So your code should look like this

module.exports = {


  friendlyName: 'Testy',


  description: 'Testy something.',


  sync: true, // Here is essential part


  inputs: {

    bla: {
      type: 'string'
    }

  },


  exits: {

    success: {

    }

  },


  fn: function (inputs, exits) {

    console.log({blabla:inputs.bla})

    if(!inputs.bla) return exits.error(new Error('text not found'));

    var h = "Hello "+ inputs.bla;

    // All done.
    return exits.success(h);

  }


};

From the another side, you have a problem with async/await. The top most reason for this are

  1. Not supported Node.js version - check that you current version support it
  2. If you use sails-hook-babel or another Babel related solution, you may miss required plugin for async/await processing
like image 101
Pavlo Zhukov Avatar answered Nov 18 '22 22:11

Pavlo Zhukov