gpt4 book ai didi

javascript - 风 sails js : How can I use service method in policy

转载 作者:行者123 更新时间:2023-12-03 11:36:26 25 4
gpt4 key购买 nike

我使用的是 sails v0.10。我在/service/Utility.js 中创建了一个服务,我想在/policies/addWhere.js 中使用 Ulity.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']

我想知道我做错了什么。

最佳答案

试试这个:

var Utility = require('../services/util');
module.exports = function(req, res, next) {
var action = Utility.getRequestAction(req);
console.log("Request action is", action);
}
next();
};

关于javascript - 风 sails js : How can I use service method in policy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26478917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com