Node.js 创建与Mongoose的聊天会话
我正在尝试创建一个消息应用程序,但我希望可以创建一个聊天会话集合,其中包含用户发送的消息和其他用户接收的消息。但是,每当发送消息时,我的应用程序都会不断生成新的聊天会话对象。甚至可以用猫鼬来做这件事吗 [![在此处输入图像描述][1][1]Node.js 创建与Mongoose的聊天会话,node.js,mongoose,ejs,Node.js,Mongoose,Ejs,我正在尝试创建一个消息应用程序,但我希望可以创建一个聊天会话集合,其中包含用户发送的消息和其他用户接收的消息。但是,每当发送消息时,我的应用程序都会不断生成新的聊天会话对象。甚至可以用猫鼬来做这件事吗 [![在此处输入图像描述][1][1] <div class="chat-window"> <% messagesArray.forEach(function(sentMessage){ %> <% if
<div class="chat-window">
<% messagesArray.forEach(function(sentMessage){ %>
<% if (sentMessage.chatSession.message != 0){ %>
<span class="chat-message"><%= sentMessage.chatSession.message %></span>
<br>
<% } %>
<% if (sentMessage.chatSession.otherMessage != 0){ %>
<span class="chat-message-other"><%= sentMessage.chatSession.otherMessage %></span>
<br>
<% } %>
<% }); %>
</div>
app.get("/chat", function(req, res) {
if(req.isAuthenticated()){
Chat.find({}, function(err, foundMessages){
res.render("chat", {messagesArray: foundMessages});
});
}else{
res.redirect("/");
}
});[![enter image description here][2]][2]
app.post("/chat", function(req,res){
const newChat = new Chat({
chatSession:{
message: req.body.message,
otherMessage: req.body.otherMessage
}
});
newChat.save(function(err){
if(err){
console.log(err);
}else{
res.redirect("/chat");
}
});
});
[1]: https://i.stack.imgur.com/vZE9Q.png