8

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.

Beaurocks16
  • 341
  • 1
  • 7
  • 16

1 Answers1

17

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");
});
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • Thanks, i'll add that now! – Beaurocks16 May 25 '13 at 15:47
  • Ok, one more problem. I do that, all the updates are sent, everything is fine except that it doesn't appear to be sorting. .sort('-date') and .sort('date') or no .sort at all gives the same results. Can you shed some light on this? – Beaurocks16 May 25 '13 at 16:00
  • @Beaurocks16 Do you have a `date` field defined in your schema and in the `Post` documents that contains the date of each doc that you're looking to sort by? – JohnnyHK May 28 '13 at 03:12
  • 1
    @Beaurocks16 Then sorting on it isn't going to work very well. :) You'll either need to add and populate that field, or sort on `_id` instead if its approximation of creation time serves your purposes. – JohnnyHK May 28 '13 at 14:13
  • That helped me! Ty! – Muhammad Haseeb May 25 '19 at 16:23