Socket.io不保存在Mongoose上save()
我创build一个简单的聊天只是为了学习socket.io以及如何在MongoDb上保存MongoDb的数据。
经过一番研究,我仍然阻塞,因为mongoose不保存数据。 在这里我的configuration:
// configuration var app = require('express')(); http = require('http').Server(app), io = require('socket.io')(http), mongoose = require('mongoose'); // connect to db mongoose.connect('mongodb://localhost/chat', function(err){ if(err){ console.log(err); }else{ console.log('Connected to MongoDb!'); } }); // Schema model var chatSchema = mongoose.Schema({ msg: String, created: {type: Date, default: Date.now} }, { collection: 'chat' }); // model var Chat = mongoose.model('Message', chatSchema); // socket.io io.on('connection', function(socket){ socket.on('chat message', function(msg){ var newMsg = new Chat({msg: msg}); newMsg.save(function(err, product, numberAffected){ if(err){ console.log(err); }else{ io.emit('chat message', msg); // console.log prints everything correctly console.log('saved: ' + product + ' - ' + numberAffected); } }); }); });
我没有任何错误在我的控制台,但如果我在我的数据库中find一些logging(通过terminal:db.chat.find())我有空值。
chat
是你的数据库的名称和集合称为Message
。 你应该使用这样的terminalfind你的logging:
use chat db.Message.find()
- 如何将返回值推入数组mongodb?
- 同步函数调用nodejs mongodb驱动程序
- 在没有GridFS的情况下,在NodeJS中使用MongoDB存储一些小(不到1MB)的文件
- 将运行Node.js脚本作为CRON作业,而不closures数据库连接最终会杀死所有的RAM?
- 如何创build一个Mongoose方法来写图像到cloudinary
- 清理closuresexpress.js + mongodb服务器(使用node.js)
- 相当于Node.js中的mongo shell db.collection.runCommand()
- 正确的方式使用.populate()mongodb Node.js
- updateOne删除我的数据