gpt4 book ai didi

node.js - Node 服务器在超过 1000 个项目的对象上崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 22:55:27 25 4
gpt4 key购买 nike

我有一个包含超过 1000 个项目的集合(1000 个项目以下是可以的。),当通过 ajax 调用请求时,这会导致以下错误并崩溃:( Node )警告:检测到递归 process.nextTick。这将在下一版本的 Node 中打破。请使用 setImmediate 进行递归延迟。

这不会发生在本地开发中。 (实际上与本地开发无关。我在本地计算机上运行 v0.10.0 之前的版本,此问题是 10.0 后的问题)

我正在使用 mongodb 数据库和 mongoose 运行 Node v0.10.8。

但是最好的解决方案是什么?

查看代码:

 Collection.find().select('_id', 'first_name').sort('startTime', -1).exec(function(err,                 

docs){

var data = {'aaData' : docs};
res.send(data);

});

最佳答案

结果集中有这么多文档,您应该使用 Mongoose 对 streaming 的支持查询的结果而不是将其放入一个大数组中。

var stream = Collection.find()
.select('_id', 'first_name')
.sort('startTime', -1)
.stream();

stream.on('data', function (doc) {
// do something with the doc like res.write(doc);
}).on('error', function (err) {
// handle the error
}).on('close', function () {
// the stream is closed, so complete the response with res.end();
});

关于node.js - Node 服务器在超过 1000 个项目的对象上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17689027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com