【问题标题】:Loopback how to handle RangeError: Maximum call stack size exceeded?Loopback 如何处理 RangeError:超出最大调用堆栈大小?
【发布时间】:2018-11-01 08:48:21
【问题描述】:

我需要在整个环回应用程序中访问一些数据。为此,我刚刚在 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/ACLs?filter={%22where%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) 在/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) 在/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) 在/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) 在/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) 在/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

由于我有多个模型,因此对于不同的文件,我得到相同的最大大小超出错误。

我用过

  1. mongodb v3.6
  2. 环回 v3
  3. 角4

请有人帮助或指导,可能的原因是什么?

【问题讨论】:

    标签: loopbackjs


    【解决方案1】:

    据我所知,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
                }
            });
    

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 1970-01-01
      • 2021-05-30
      • 2021-07-11
      • 2019-07-05
      • 2015-04-26
      • 2014-06-11
      相关资源
      最近更新 更多