Sails.js template helper (filter) with promise



我刚刚进入Sails.js,想知道是否有一种方法可以让EJS模板系统运行一个包含承诺或数据库查询的helper函数。如此:

在config/http.js:

module.exports.http = {
  // ...
  locals: {
    filters: {
      testHelper: function(input) { return input; }
    }
  }
}

module.exports.http = {
  // ...
  locals: {
    filters: {
      testHelper: function(input) {
            MyModal.create({test:'test'}).exec(function(err,mm){
                  return input+'-'+mm.test;
              });
          }
    }
  }
}

第一个总是返回"check",第二个总是返回"undefined"当在模板中使用"{{ 'check' | testHelper }}"

您可以使用像wait这样的库。为了包装和返回的承诺。我建议将"helper"创建为服务,并在全局作用域之外访问它,而不是在视图中注入它。

最新更新