问题描述
我正在建立一个简单的小型社交网络.我已经完成了所有输入帖子,用户等.现在唯一的错误是它几乎只是一个聊天室.每当您在某人的页面上发布某项内容时,当时只有该页面上的人可以查看.刷新后,所有帖子都消失了.这是发帖时我正在做的技术部分,以及我想做的事情.
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.
无论何时发布帖子,它所做的事情都不重要,我不会列出它们.但是有一部分很重要,将其发送到NodeJS服务器.这是它使用的代码:
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
}
如您所见,它发出一个带有用户名,uid,内容和页面的"newPost".在服务器上,它接收与此相关的帖子,然后插入数据库中.
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
我的两个问题是:我如何使它找到最新的帖子?为什么只有这一个,它会返回"null"?
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.
推荐答案
在execFind
回调中,post
参数是一组帖子,而不仅仅是一个帖子.这就是为什么当您尝试将其视为单个帖子时得到null says
的原因.
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.
此外,如果您只希望使用最近的10个,则可以在查询链中调用limit(10)
.您可能还应该使用exec
而不是execFind
,因为它更清晰一些.
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.
所以像这样:
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");
});
这篇关于猫鼬在数据库中找到最后十个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!