我正在使用风帆v0.10。我在/service/Utility.js中创建了一个服务,我想在/picies/addWhere.js中使用Ulitity.js方法 我想知道是否可以在政策中使用服务。
我的Utility.js看起来像是:
var util = require('util'),
_ = require('lodash');
module.exports = {
getRequestAction: function (req) {
if (req.options.action) return req.options.action;
var controller = req.options.controller || req.options.model;
var baseRoute = sails.config.blueprints.prefix + controller;
var requestRoute = req.route.method + ' ' + req.route.path;
var Model = sails.models[controller];
if (req.options.shortcuts && Model) {
var shortcutRoutes = {
'/%s/find/:id?': 'find',
'/%s/create': 'create',
'/%s/update/:id?': 'update',
'/%s/destroy/:id?': 'destroy'
};
var shortcutAction = _.findWhere(shortcutRoutes, function(blueprint, pattern){
var shortcutRoute = util.format(pattern, baseRoute);
return req.route.path === shortcutRoute;
});
if (shortcutAction) return shortcutAction;
}
if (req.options.rest && Model) {
var restRoutes = {
'get /%s/:id?': 'find',
'post /%s': 'create',
'put /%s/:id?': 'update',
'delete /%s/:id?': 'destroy'
};
var restAction =_.findWhere(restRoutes, function(blueprint, pattern){
var restRoute = util.format(pattern, baseRoute);
return requestRoute === restRoute;
});
if (restAction) return restAction;
var associationActions = _.compact(_.map(req.options.associations, function(association){
var alias = association.alias;
var associationRoutes = {
'get /%s/:parentid/%s/:id?': 'populate',
'post /%s/:parentid/%s': 'add',
'delete /%s/:parentid/%s': 'remove'
};
return _.findWhere(associationRoutes, function(blueprint, pattern){
var associationRoute = util.format(pattern, baseRoute, alias);
return requestRoute === associationRoute;
});
}));
if (associationActions.length > 0) return associationActions[0];
}
}
};
addWhere.js就像:
var Utility=require('util');
module.exports = function(req, res, next) {
var action = Utility.getRequestAction(req);
console.log("Request action is", action);
}
next();
};
但是当我尝试执行它时,返回错误:
error: Sending 500 ("Server Error") response:
TypeError: Object #<Object> has no method 'getRequestAction'
at module.exports (/var/www/sails/LayupSailsServer/api/policies/addWhere.js:7:26)
at routeTargetFnWrapper (/var/www/sails/LayupSailsServer/node_modules/sails/lib/router/bind.js:178:5)
at callbacks (/var/www/sails/LayupSailsServer/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
at param (/var/www/sails/LayupSailsServer/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
at pass (/var/www/sails/LayupSailsServer/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
at nextRoute (/var/www/sails/LayupSailsServer/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
at callbacks (/var/www/sails/LayupSailsServer/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
at /var/www/sails/LayupSailsServer/node_modules/sails/lib/router/bind.js:186:7
at module.exports (/var/www/sails/LayupSailsServer/api/policies/isAuthorized.js:6:5)
at routeTargetFnWrapper (/var/www/sails/LayupSailsServer/node_modules/sails/lib/router/bind.js:178:5) [TypeError: Object #<Object> has no method 'getRequestAction']
我想知道自己做错了什么。
答案 0 :(得分:1)
试试这个:
var Utility = require('../services/util');
module.exports = function(req, res, next) {
var action = Utility.getRequestAction(req);
console.log("Request action is", action);
}
next();
};
答案 1 :(得分:0)
这项服务看起来很好,即使在我看来还有很多重新发明轮子的事情。
错误在于:
var Utility=require('util');
你基本上用这行代码用util对象覆盖服务。
为了清晰的代码,我建议使用服务后缀命名策略。因此,在这种情况下utilityService.js
代替Utility.js
然后你在代码中的任何地方utilityService.getRequestAction()
做,你不需要任何东西。
但我对帆很新,所以可能会有一些我看不到的东西。
编辑:当前操作和控制器名称应该可以通过req.options访问... sails.log.debug(JSON.stringify(req.options));
答案 2 :(得分:0)
addWhere.js
要调用的ServiceFileName.action
module.exports = function(req, res, next) {
var action = Utility.getRequestAction(req);
console.log("Request action is", action);
};
Utility.js (因为lodash是帆中的全球对象)
var util = require('util');
module.exports = {
getRequestAction: function (req) {