NicolasSiver / nodebb-plugin-ns-points

Additional metrics for User Profiles. It's like experience in video games. Plugin is good for gamification of your board.
MIT License
22 stars 20 forks source link

Post object is null. Plugin Crashes forum #16

Closed fais3000 closed 5 years ago

fais3000 commented 5 years ago

Nodebb 1.12.1 MongoDB 3.4

@NicolasSiver There is something in our database that makes this plugin and ns-awards crash when certain calculation is made. Which leads to crashing whole nodebb server.

Here is the log of area where it crashes

2019-04-16T22:09:48.190Z [4567/24494] - error: uncaughtException: Cannot read property 'uid' of null
TypeError: Cannot read property 'uid' of null
    at /var/www/community/node_modules/nodebb-plugin-ns-points/plugin/filters.js:67:37
    at /var/www/community/node_modules/async/dist/async.js:1135:9
    at eachOfArrayLike (/var/www/community/node_modules/async/dist/async.js:1069:9)
    at eachOf (/var/www/community/node_modules/async/dist/async.js:1117:5)
    at _asyncMap (/var/www/community/node_modules/async/dist/async.js:1133:5)
    at Object.map (/var/www/community/node_modules/async/dist/async.js:1122:16)
    at Object.Filter.postGetPosts [as method] (/var/www/community/node_modules/nodebb-plugin-ns-points/plugin/filters.js:66:15)
    at /var/www/community/src/plugins/hooks.js:136:12
    at /var/www/community/node_modules/async/dist/async.js:2516:9
    at replenish (/var/www/community/node_modules/async/dist/async.js:1011:17) {"error":{},"stack":"TypeError: Cannot read property 'uid' of null\n    at /var/www/community/node_modules/nodebb-plugin-ns-points/plugin/filters.js:67:37\n    at /var/www/community/node_modules/async/dist/async.js:1135:9\n    at eachOfArrayLike (/var/www/community/node_modules/async/dist/async.js:1069:9)\n    at eachOf (/var/www/community/node_modules/async/dist/async.js:1117:5)\n    at _asyncMap (/var/www/community/node_modules/async/dist/async.js:1133:5)\n    at Object.map (/var/www/community/node_modules/async/dist/async.js:1122:16)\n    at Object.Filter.postGetPosts [as method] (/var/www/community/node_modules/nodebb-plugin-ns-points/plugin/filters.js:66:15)\n    at /var/www/community/src/plugins/hooks.js:136:12\n    at /var/www/community/node_modules/async/dist/async.js:2516:9\n    at replenish (/var/www/community/node_modules/async/dist/async.js:1011:17)","exception":true,"date":"Tue Apr 16 2019 22:09:48 GMT+0000 (UTC)","process":{"pid":24494,"uid":0,"gid":0,"cwd":"/var/www/community","execPath":"/root/.nvm/versions/node/v8.11.4/bin/node","version":"v8.11.4","argv":["/root/.nvm/versions/node/v8.11.4/bin/node","/var/www/community/app.js"],"memoryUsage":{"rss":438337536,"heapTotal":304680960,"heapUsed":255611952,"external":54099262}},"os":{"loadavg":[1.5625,1.14306640625,1.07568359375],"uptime":7181824},"trace":[{"column":37,"file":"/var/www/community/node_modules/nodebb-plugin-ns-points/plugin/filters.js","function":null,"line":67,"method":null,"native":false},{"column":9,"file":"/var/www/community/node_modules/async/dist/async.js","function":null,"line":1135,"method":null,"native":false},{"column":9,"file":"/var/www/community/node_modules/async/dist/async.js","function":"eachOfArrayLike","line":1069,"method":null,"native":false},{"column":5,"file":"/var/www/community/node_modules/async/dist/async.js","function":"eachOf","line":1117,"method":null,"native":false},{"column":5,"file":"/var/www/community/node_modules/async/dist/async.js","function":"_asyncMap","line":1133,"method":null,"native":false},{"column":16,"file":"/var/www/community/node_modules/async/dist/async.js","function":"Object.map","line":1122,"method":"map","native":false},{"column":15,"file":"/var/www/community/node_modules/nodebb-plugin-ns-points/plugin/filters.js","function":"Object.Filter.postGetPosts [as method]","line":66,"method":"Filter.postGetPosts [as method]","native":false},{"column":12,"file":"/var/www/community/src/plugins/hooks.js","function":null,"line":136,"method":null,"native":false},{"column":9,"file":"/var/www/community/node_modules/async/dist/async.js","function":null,"line":2516,"method":null,"native":false},{"column":17,"file":"/var/www/community/node_modules/async/dist/async.js","function":"replenish","line":1011,"method":null,"native":false}]}
2019-04-16T22:09:48.193Z [4567/24494] - error: Cannot read property 'uid' of null
2019-04-16T22:09:48.193Z [4567/24494] - info: [app] Shutdown (SIGTERM/SIGINT) Initialised.

Within /nodebb-plugin-ns-points/plugin/filters.js:67

Filter.postGetPosts = function (payload, callback) {
        async.map(payload.posts, function (post, next) {
            database.getPoints(post.uid, function (error, points) {
                if (error) {
                    return next(error);
                }
                post.points = points || 0;
                next(null, post);
            });
        }, function (error, results) {
            if (error) {
                return callback(error);
            }
            payload.posts = results;
            callback(null, payload);
        });
    };

So some how the post object is null. Is there any way we can catch this error and fail gracefully and not crash the whole server? Or alternatively any way we can run a query to find this dataset to fix the issue which causes the crash?

fais3000 commented 5 years ago

For anyone else having this issue. I've added an if condition here, which solves the crashes https://github.com/fais3000/nodebb-plugin-ns-points/commit/65338e0d3f1f9709b66b613cf05ee8ac94d02dae