I want to make a page where you can see all the users registered to the website. The url for that is /users. When someone goes there, the server searches the database for all the users and returns an array of objects. But I want to make it so that I have more than one pages for that (pagination/ kind of like google and amazon etc.). Page 1, page 2, page 3 and so on. So, I splice() the array of objects returned by the previous function. When I console.log() it the array seems to be ok, but when I got the /users page it says it can't find the array (but the array is normally passed to the client). When I don't have the splice method, it works ok, but I still want to have the pagination. Here's the code:
app.get('/users', function(req, res, next){
var usersArray = [];
Users.find({}, function(err, users){
if(err) throw err;
if(users) usersArray = users; // this works ok
if(!users) res.send('Found no users!');
})
usersArray.splice(10, usersArray.length); // for the first page, and only show them 10 users
// I haven't written the code for the second page yet, since this doens't work
res.render('users.ejs', {
usersArray: usersArray // it gets passed normally
})
}
The client renders the array in some styled html with ejs. The problem is userData not found, on line .....
The user schema is like this:
var userSchema = mongoose.Schema({
userData: {
(and all the data about them in here)
}
});
Again, it works ok, as long as I don't splice the returned array! Any help? Thank you.