我需要在loopback应用程序中访问一些数据。为此,我刚刚在accessToken中附加了数据。并且可以很方便。
但是有一段时间我的linux服务器被关闭了。在查看永远日志时,我发现了一些错误,即超出最大通话大小。
module.exports = function (options) {
return function storeCurrentUser(req, res, next) {
if (!req.accessToken) {
next();
}else{
app.models.User.findById(req.accessToken.userId, function (err, user) {
if (err) {
next(err);
}
if (!user) {
next(new Error('No user with this access token was found.'));
}else{
var PM = app.registry.getModel("PersistedModel");
PM.observe("access", function (ctx, next) {
ctx.options.data = req.accessToken.data; // each error showing this line
next();
});
next();
}
});
}
};
};
有请求GET / API / ACL可以使用未处理的= {误差%二条%22:{%22principalId%22:%22admin%22}}&安培; =的access_token 4N8gnOrgGUpjVDohYsNj9pWBruUFhif8NCjg95RoITxU1xDGwvcgFwTGjxNbqs9C:?的RangeError:最大调用堆栈大小超过 在补充(/home/ubuntu/hms/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:870:27) 在iterateeCallback(/home/ubuntu/hms/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:866:17) at /home/ubuntu/hms/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:843:16 在/home/ubuntu/hms/server/middleware/store-current-user.js:62:25 在notifySingleObserver(/home/ubuntu/hms/node_modules/loopback-datasource-juggler/lib/observer.js:160:22) at /home/ubuntu/hms/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:3025:16 在补充(/home/ubuntu/hms/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:881:17) 在iterateeCallback(/home/ubuntu/hms/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:866:17) at /home/ubuntu/hms/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:843:16 在/home/ubuntu/hms/server/middleware/store-current-user.js:62:25 在notifySingleObserver(/home/ubuntu/hms/node_modules/loopback-datasource-juggler/lib/observer.js:160:22) at /home/ubuntu/hms/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:3025:16 在补充(/home/ubuntu/hms/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:881:17) 在iterateeCallback(/home/ubuntu/hms/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:866:17) at /home/ubuntu/hms/node_modules/loopback-datasource-juggler/node_modules/async/dist/async.js:843:16 at /home/ubuntu/hms/server/middleware/store-current-user.js:62:25
由于我有多个模型,因此不同文件的最大大小超出错误。
我用过
- mongodb v3.6
- loopback v3
- angular 4
醇>
请有人帮忙或指导,可能的原因是什么?
答案 0 :(得分:0)
显然PM.observe的问题就像我理解的那样在每次请求时被调用,因此对于每个请求你都有一个+1的监听器模型。
最好将上下文用于此类目的 https://loopback.io/doc/en/lb3/Using-current-context.html
例如: server.js或中间件
app.remotes().phases.addBefore('invoke', 'options-from-request').use(function(ctx, next) {
ctx.shared = {};
let promise;
let accessToken = ctx.req.accessToken;
if (accessToken) {
promise = app.models.OrmUser.findOne({
where: {
id: accessToken.userId
}
});
promise.then(function(result) {
if (!result) {
return next(new Error('No user with this access token was found.')) || null;
}
ctx.shared.currentUser = result;
return Promise.resolve(result);
}).catch(function(err) {
return next(err);
});
} else {
promise = Promise.resolve(null);
}
promise.then(function(result) {
let options = ctx.args.options || {};
options.remoteCtx = ctx;
ctx.args.options = options;
return next() || null;
}).catch(function(err) {
return next(err) || null;
});
});
你的模特:
Model.process = function(data, options, cb) {
if (typeof options == 'function') {
cb = options;
options = {};
}
cb = cb || utils.createPromiseCallback();
// from context
let remoteCtx = options && options.remoteCtx;
let shared = remoteCtx && remoteCtx.shared;
let currentUser = shared && shared.currentUser;
cb(new Error('no impl'));
return cb.promise;
};
Model.remoteMethod('process', {
accessType: 'WRITE',
accepts: [{
arg: 'data',
type: 'object',
required: true,
http: {
source: 'body'
}
}, {
arg: 'options',
type: 'object',
http: 'optionsFromRequest'
}],
http: {
path: '/process',
verb: 'post'
},
returns: {
type: Model.modelName,
root: true
}
});