我尝试通过我的用户获取一些统计信息。这是我尝试这样做的方式:
function(cb) { // Get users count + blocked + Abos
console.log('---- launching count ----');
User.find({pageId: pageId}).exec(function(error, _users) {
if (error) return cb(error);
req._locals.usersCount = _users.length;
console.log('---- userCount:', _users.length, ' ----' );
return cb(_users.map((_user) => {
if (_user.getVariable('tip-subscription-optin') !== null)
req._locals.tipsSubscriptions++;
if (_user.getVariable('tip-mercredi-subscription-optin') !== null)
req._locals.greenSubscriptions++;
}));
});
},
但这不起作用。这是错误:
---- launching count ----
<--- JS stacktrace --->
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
正如你所看到的,我的第一个console.log
正在工作但不是我的第二个。所以我很确定mongoose.find
请求对我的服务器来说太大了。
虽然我自己还在了解这个问题,但是你的查询已经耗尽内存来处理数据,并导致堆溢出。从技术上讲,你在相同的堆栈级别上调用此(users.length
)两次(我认为..)
req._locals.usersCount = _users.length;
console.log('---- userCount:', _users.length, ' ----' );
所以你的堆(你的RAM上的可用内存)已经耗尽内存以分配给你的堆栈(函数使用的是什么)..如果你删除变量req._locals.usersCount = _users.length;
它会工作吗?这应该减少函数调用所需的内存分配
(另外,其他人,请纠正我,我还在学习这个,这对我很有意思:))