我在尝试在模型的 RemoteMethods 中恢复我的 token 的用户 ID 时遇到问题。我使用了以下代码:
User.beforeRemote('**', function(ctx, user, next) {
//...some custom token decoding
ctx.req.body.user_id = token.user_id;
console.log('token : ', token);
next();
});
然后,在我的模型中我使用了:
User.check = function (user_id, cb) {
// Do stuff..
};
User.remoteMethod(
'check',
{
http: {path: '/check', verb: 'post'},
returns: {arg: 'rate', type: 'object'},
accepts: [
{
arg: 'user_id',
type: 'number',
required: true,
description: "id of the user to check",
http: function getUserid(ctx) {
console.log('User.check http body : ', ctx.req.body);
return ctx.req.body.user_id;
}
}
],
description: ''
}
);
问题是我的参数的函数“getUserid”在“User.beforeRemote”调用之前被触发。
这是一个错误吗?你知道我如何才能做到这一点吗? 我不想使用
arg : {http : {source : 'body'}},
因为我只想在远程方法中包含 user_id arg,并且因为我必须在大约 20~30 个现有方法中执行此操作
谢谢!
最佳答案
我终于找到了一种更简单的方法: 我在中间件中添加了 token 解码,它工作得很好,具有经典的 arg 类型号,没有 http 函数:
//middleware dir :
module.exports = function (app) {
"use strict";
return function (req, res, next) {
//Custom token decoding code ....
//used req.body to show example with post requests :
req.body.user_id = parseInt(token.user_id);
console.log('token : ', token);
next();
};
};
//remote Method declaration, without need to add a beforeRemote method :
User.remoteMethod(
'check',
{
http: {path: '/check', verb: 'post'},
returns: {arg: 'rate', type: 'object'},
accepts: [
{
arg: 'user_id',
type: 'number',
required: true,
description: "id of the user to check"
}
],
description: ''
}
);
关于javascript - 环回在 header 中使用标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39527763/