node.js表达mongodb保存消息? [英] node.js express mongodb save messages?

查看:102
本文介绍了node.js表达mongodb保存消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以一种非常简单的方式来查看express,mongoose和mongodb.如果我可以放一个盒子,把我的名字放在那里,提交并保存到数据库,这就是我所需要的.我尝试通过学习一些教程来做到这一点,但是我陷入了困境.我试图通过聊天来完成,只保存每条消息.如果您能以自己的方式使我的示例发挥作用或拥有自己的示例,那么我不仅会帮助我看到有用的内容.然后,我就可以知道什么可以起作用,并可以对其进行补充.我看到了很多教程以及其他所有内容,但对人们来说却不是一件简单的事情,它总是一个大项目,您迷失在某个地方,希望这对其他人也有帮助,它涉及到很多东西,包括节点和所有它的朋友.以我的示例为例,我没有看到任何错误,但是也许有些错误.而且我正在数据库聊天中使用db.messages.find()来查看消息,但是那里没有.

I'm trying to see express, mongoose and mongodb work togetter, in a really simple way. If I can have a box, put my name in there, submit it and have it save to the db, that's all i need. I tried doing this from taking pieces of tutorials but I'm stuck. I was trying to do it through a chat and just save every message. If you can get my example to work or have one of your own, either way, I't just helps me see something that works. Then I can have a foundation of what works and can add to it. I see a lot of tutorials and everything else, but not something simple for people, it's always a big project you get lost on somewhere, hopefully this will be helpfully to others too, it's a lot, with everything, with node and all it's friends. With my example I am not getting any errors, that I see, but there maybe some. And I'm looking in db chat to see the messages using db.messages.find() but there not in there.

我的html

<html>
<head>
<title>Chat with socket.io and node.js</title>
<style>
#chat{
height:500px;
}
</style>
</head>
<body>
<div id="chat"></div>
<form id="send-message">
<input size="35" id="message"></input>
<input type="submit"></input>
</form>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
jQuery(function($){
var socket = io.connect();
var $messageForm = $('#send-message');
var $messageBox = $('#message');
var $chat = $('#chat');

$messageForm.submit(function(e){
e.preventDefault();
socket.emit('send message', $messageBox.val());
$messageBox.val('');
});

socket.on('new message', function(data){
$chat.append(data + "<br/>");
});
});

var chatSchema = mongoose.Schema({
msg: String,
created: {type: Date, default: Date.now}
});

var Chat = mongoose.model('Message', chatSchema);

var newMsg = new Chat({msg: msg});
        newMsg.save(function(err){
            if(err) throw err;
});               


</script>
</body>
</html>

应用

var mongoose = require('mongoose')
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);

server.listen(3000);

app.get('/', function(req, res){
res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function(socket){
socket.on('send message', function(data){
io.sockets.emit('new message', data);
});
});

mongoose.connect('mongodb://localhost/chat', function(err){
if(err){
console.log(err);
} else{
    console.log('Connected to mongodb!');
}
});

推荐答案

我已经尝试过了,修改后的版本也行得通!主要的更改是将Mongoose模式代码放在服务器端,并仅在成功保存到Mongo数据库中之后,在服务器上回显文本框的内容.

I've tried it and the modified version bellow works! The main changes where putting the mongoose schema code on the server side, and echo back the content of the text box on the server only after a successful save in the Mongo database.

还请参见 mean.io 网站,以介绍MEAN堆栈,如果您有任何疑问,请告诉我在代码上.

Have a look also at the mean.io site for introduction to the MEAN stack, let me know if you have questions on the code.

修改后的server.js:

Modified server.js:

var mongoose = require('mongoose')
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);

server.listen(3000);

app.get('/', function(req, res){
    res.sendfile(__dirname + '/test.html');
});

io.sockets.on('connection', function(socket){
    socket.on('send message', function(data){

        var newMsg = new Chat({msg: '' + data});

        console.log('saving newMsg: ' + newMsg);

        newMsg.save(function(err){
            console.log('saved, err = ' + err);
            if(err) throw err;
            console.log('echoeing back data =' + data);
            io.sockets.emit('new message', data);
        });

    });
});

var chatSchema = mongoose.Schema({
    msg: String,
    created: {type: Date, default: Date.now}
});

var Chat = mongoose.model('Message', chatSchema);



mongoose.connect('mongodb://localhost/test', function(err){
if(err){
console.log(err);
} else{
    console.log('Connected to mongodb!');
}
});

这是html页面:

<html>
    <head>
        <title>Chat with socket.io and node.js</title>
        <style>
            #chat{
                height:500px;
            }
        </style>
    </head>
    <body>
        <div id="chat"></div>

        <form id="send-message">
            <input size="35" id="message"></input>
            <input type="submit"></input>
        </form>

        <script src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script src="/socket.io/socket.io.js"></script>

        <script>
            jQuery(function($){
                var socket = io.connect();
                var $messageForm = $('#send-message');
                var $messageBox = $('#message');
                var $chat = $('#chat');

                $messageForm.submit(function(e){
                    e.preventDefault();
                    socket.emit('send message', $messageBox.val());
                    $messageBox.val('');
                });

                socket.on('new message', function(data){
                    console.log('Received data: ' + data);
                    $chat.append(data + "<br/>");
                });
            });

        </script>

    </body>
</html>

这篇关于node.js表达mongodb保存消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆