sails.js – 帆助手和机器规格

我将sails升级到@ ^ 1.0.0版本,当我正在开发API时,我想使用Service但Sails文档建议现在使用Helper.而且我不会真正使用新方法来描述助手,构建脚本或操作.

我疯狂的所有尝试都没有成功.

以下例子..

这是我的控制器电话:

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

    res.json({ob:ob});

帮手

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);

}

};

我收到了这个错误

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.......

如果我从Controller中删除“async”和“await”,ob对象返回null并且我有这个错误

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)

最佳答案 评论中的第一个人是对的.

从fn:async函数中删除异步后(输入,存在){};你需要设置sync:true默认为false.在Synchronous助手部分描述了at helpers doc page.

所以你的代码应该是这样的

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);

  }


};

从另一方面来看,你遇到了async / await的问题.最主要的原因是

>不支持Node.js版本 – 检查您当前的版本是否支持它
>如果您使用sails-hook-babel或其他与Babel相关的解决方案,您可能会错过异步/等待处理所需的插件

点赞