您是否尝试使用 Node.js 和 MongoDB 实现分页?
您的参数在错误的位置(不是第一个参数find()
)。这是我的 HackHall GitHub 链接代码:
exports.getPosts = function(req, res, next) {
var limit = req.query.limit || LIMIT;
var skip = req.query.skip || SKIP;
req.db.Post.find({}, null, {
limit: limit,
skip: skip,
sort: {
'_id': -1
}
}, function(err, obj) {
if (!obj) next('There are not posts.');
obj.forEach(function(item, i, list) {
if (req.session.user.admin) {
item.admin = true;
} else {
item.admin = false;
}
if (item.author.id == req.session.userId) {
item.own = true;
} else {
item.own = false;
}
if (item.likes && item.likes.indexOf(req.session.userId) > -1) {
item.like = true;
} else {
item.like = false;
}
if (item.watches && item.watches.indexOf(req.session.userId) > -1) {
item.watch = true;
} else {
item.watch = false;
}
});
var body = {};
body.limit = limit;
body.skip = skip;
body.posts = obj;
req.db.Post.count({}, function(err, total) {
if (err) next(err);
body.total = total;
res.json(200, body);
});
});
};
有关更多信息,请查看我在webapplog.com和Express.js Guide上的其他帖子。