Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 猫鼬_Node.js_Angularjs_Mongoose - Fatal编程技术网

Node.js 猫鼬

Node.js 猫鼬,node.js,angularjs,mongoose,Node.js,Angularjs,Mongoose,我正在使用一个MongoDB数据库。 对于前部角度js 此外,我正在使用猫鼬 我有: app.get("/images", function(req, res) { mongoose.model('Image').find(function (err, images) { res.send(images); }); }); app.listen(3000); 当我转到:http://localhost:3000/images 然后: {{photo.name}

我正在使用一个MongoDB数据库。 对于前部角度js

此外,我正在使用猫鼬

我有:

app.get("/images", function(req, res) {
    mongoose.model('Image').find(function (err, images) {
        res.send(images);
    });
});
app.listen(3000);
当我转到:
http://localhost:3000/images

然后:


{{photo.name}
在这里之前,一切都很好

但是我将有成千上万的数据要显示。 因此,我需要像“无限滚动”这样的东西作为前端,同时我还需要限制API
http://localhost:3000/images
是否具有查询范围或分页

如何处理大量数据


谢谢

在mongoose中使用服务器端分页,并限制查询输出。在角度使用中包含分页的方式相同。如果你想使用无限滚动,那么在角度使用


希望这有帮助。

您可以使用mongoose查询API指定搜索条件:

您可以使用
where()
来指定分页条件,例如
where({id:{$gt:page}})
(假设您使用某种自动递增的id)和
limit()
来指定页面大小,例如
limit(10)

每次向服务器查询附加页面时,都需要在URL中提供条件:
/images?page=1
。每次获取页面时,此条件应递增10


标准可以是表示数据顺序的任何内容,无论是ID、时间戳还是其他任意值。

要限制mongoose中的数据:

mongoose.model('Image').limit(10).exec(function(err, images){
  // get the images....
}); // this will only return 10 images
在角度模式下,您可以使用
limito
过滤器:

<div ng-repeat="photo in photos | limitTo: 10 ">
   <!-- My Data List -->
   <h3>{{photo.name}}</h3>
</div>

{{photo.name}

现在,您可以学习如何分页或无限滚动。(网络上有很多关于这个主题的tut)

如果数据量很大,你可能不想一次加载所有数据。 首先,我会这样做,限制每个请求的条目数

app.get("/images", function(req, res) {
    var query = mongoose.model('Image').find({})
                .limit(req.query.numberOfItems)
                .skip(req.query.index)

    query.exec(function (err, images) {
        res.send(images);
    });    
});
app.listen(3000);
在客户机上,您应该决定在需要时动态加载更多数据


这取决于您希望用户体验是什么,例如,有不同的页面,打开下一页后加载更多数据,或者如果用户进一步向下滚动,等等。

谢谢,我会看一看!谢谢你的帮助!
app.get("/images", function(req, res) {
    var query = mongoose.model('Image').find({})
                .limit(req.query.numberOfItems)
                .skip(req.query.index)

    query.exec(function (err, images) {
        res.send(images);
    });    
});
app.listen(3000);