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.