Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose find last ten entries in database

I am making a simple little Social Network. I have all the inputting posts, users, etc, done. The only thing wrong right now is it is pretty much just a chat room. Whenever you post something on someone's page, the only people that can view it are the people on the page at the time. When you refresh, all the posts are gone. Here is the technical part of what I am doing when posts are sent, and what I want to do.

Whenever you post a post, it does somethings that are not important, I will not list them. But there is one part that is important, sending it to the NodeJS server. Here is the code it uses:

function sendPost(cont) {
    socket.emit("newPost", username, uid, cont, page);
    model.posts.unshift(new Post(username, uid, cont, page)); // You don't need to worry about this
}

As you can see, it emits a "newPost" with the username, uid, content and page. On the server, it receives posts with this, and then inserts into a database.

socket.on("newPost", function (name, id, cont, page) {
    var thePost = new Post({name: name, cont: cont, id: id, page: page});
    console.log("Received a new post by "+thePost.name+"(ID of "+thePost.id+"), with the content of \""+thePost.cont+"\", on the page "+thePost.page);
    socket.broadcast.emit("Post", name, id, cont, page);
    console.log("Post sent!");
    console.log("Putting post in database...");
    thePost.save(function (err, thePost) {
        if (err) {
            console.log("Error inserting into database: "+err);
        } else {
            console.log("Put into database finished!");
        }
    });
});

Now for my actual problem/question. Whenever the page loads, it sends a request to the server like this:

socket.emit("getPrevious", curPage, amount);

That works all fine. On the server, it receives that and does the following:

socket.on("getPrevious", function(curPage, amount) {
    console.log("Someone requested a page update, sending it to them...");
    Post.find({'page': curPage}).sort('-date').execFind(function(err, post){
        console.log("Emitting Update...");
        socket.emit("Update", post.name, post.id, post.cont);       
        console.log("Update Emmited");
    });
});

That code will only find One of the latest posts on that page. I want it to find the last of posts, then send them back. Even with it happening only once, when I go to the page, it will display this:

null says

My two questions are this: How would I make it find the latest of posts, and why, with only this one, does it return "null"?

Thanks it advance. If you need any code, tell me. If anything is unclear, tell me.

like image 825
Beaurocks16 Avatar asked May 23 '13 17:05

Beaurocks16


1 Answers

In the execFind callback, the post parameter is an array of posts, not just one. That's why you're getting null says when you try and treat it as a single post.

Also, if you only want the most recent 10 you can call limit(10) in your query chain. You should probably also use exec instead of execFind as it's a bit clearer.

So something like:

Post.find({'page': curPage}).sort('-date').limit(10).exec(function(err, posts){
    console.log("Emitting Update...");
    socket.emit("Update", posts.length);       
    console.log("Update Emmited");
});
like image 146
JohnnyHK Avatar answered Oct 20 '22 12:10

JohnnyHK