使用Socket.io在mongodb中插入数据

时间:2016-05-27 06:24:00

标签: node.js mongodb socket.io

我正在开发简单的聊天应用程序,我想在mongoDb中存储消息。 但是当我运行此代码时,我收到错误。 无法读取未定义的属性' _id'我已经创建了一个集合呼叫聊天。



var MongoClient= require('mongodb').MongoClient;
var assert=require('assert');
var ObjectId= require('mongodb').ObjectId;
var bodyParser= require('body-parser');	
var express = require('express'),
 	app=express();
 	server=require('http').createServer(app),
 	io= require('socket.io').listen(server),
 	app.use(bodyParser.urlencoded({extended:false}));
 	var mongo=require('mongodb');
 	users = {};
 	url='mongodb://localhost:27017/anant';

server.listen(9000, function(){
	var host= server.address().address;
	var port=server.address().port;

	console.log("save app listen on http://%s:%s", host , port);
});

/*var server = app.listen(9000, function(){
	var host= server.address().address;
	var port=server.address().port;

	console.log("save app listen on http://%s:%s", host , port);
});*/


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


io.sockets.on('connection', function(socket){
	socket.on('new user', function(data , callback){
		if(data in users){
				callback(false);
		}else{
			callback(true);
			socket.nickname= data;
			users[socket.nickname]= socket;
			updatenicknames();
		}
	});
	
	io.sockets.on('send request', function(data , callback){
			var name=data;
			console.log(data);
	});


	socket.on('send message', function(data , callback){
		var msg=data.trim();
		if(msg.substr(0,3) === '/w '){
				msg = msg.substr(3);
				var	 ind = msg.indexOf(' ');
				console.log(ind);
				console.log("got it after ind ");
				if(ind !== -1)
				{
					var  name=msg.substr(0, ind);
					var msg=msg.substring(ind + 1);
					console.log(name);
					console.log(msg);
					if(name in users){
						console.log("got it in if name condition");
						users[name].emit('wishper', {msg: msg , nick: socket.nickname} );
					console.log('whisper..!	');
					} else
					{
						callback("enter a vlid user");
					}
				} else{
						callback('please enter message for your wishper');
				}
		}else {
		insertData(data);
		io.sockets.emit('new message', {msg: msg , nick: socket.nickname} );
		}	
	});	

	function updatenicknames(){
			io.sockets.emit('usernames', Object.keys(users));
	}

	function insertData(data)
	{	
	
			MongoClient.connect(url, function(err, db){
			console.log(data);
			db.collection('chat').save(data.msg , (err,result)=>{
				if(err){
					console.log("not inserted");
				}else {
					console.log("inserted");
				}
			});
		});
		
	}
	socket.on('disconnect', function(data){
		if(!socket.nickname) return;
		delete users[socket.nickname];
		updatenicknames();
	});
});




1 个答案:

答案 0 :(得分:2)

这是因为您将字符串 SELECT wm.CID, wm.DOC_ID, wm.DOC_TYPE, wml.SERIAL_NO, wml.WH_MVT_NO, wm.PROD_ID, wm.LOT_CODE, wm.WH_CODE, wm.BIN_CODE, wml.MVT_DATE, wm.BATCH_NO, wm.MVT_TYPE, wm.PROD_NAME, wm.COMMENT, wm.REMARK, wm.NOTE, wm.INSTRUCTION FROM STOCK.WAREHOUSE_MVT_LINE wml INNER JOIN STOCK.WAREHOUSE_MVT wm ON wml.CID=wm.CID AND wml.WH_MVT_NO=wm.WH_MVT_NO WHERE wml.CID = 70200009 AND ( wm.MVT_TYPE != 'TFOUT' AND wm.MVT_TYPE != '3TFOUT' ) AND wml.SERIAL_NO IN ( ( SELECT SERIAL_NO FROM STOCK.WAREHOUSE_MVT_LINE mvtLn INNER JOIN STOCK.WAREHOUSE_MVT mvt ON mvtLn.CID = mvt.CID AND mvtLn.PFC_CODE = mvt.PFC_CODE AND mvtLn.WH_MVT_NO = mvt.WH_MVT_NO where mvtLn.CID = 70200009 AND mvtLn.PFC_CODE ='SG' AND mvtLn.MVT_DATE >= '2016-04-01 00:00:00.0' AND mvtLn.MVT_DATE <='2016-06-30 00:00:00.0' AND mvt.WH_CODE = 'IDJKT' AND mvt.BIN_CODE = 'IMP' )AS a INNER JOIN ( SELECT SERIAL_NO FROM STOCK.WAREHOUSE_MVT_LINE mvtLn INNER JOIN STOCK.WAREHOUSE_MVT mvt ON mvtLn.CID = mvt.CID AND mvtLn.PFC_CODE = mvt.PFC_CODE AND mvtLn.WH_MVT_NO = mvt.WH_MVT_NO where mvtLn.CID = 70200009 AND mvtLn.PFC_CODE ='SG' AND mvtLn.MVT_DATE >= '2016-01-01 00:00:00.0' AND mvtLn.MVT_DATE <='2016-03-31 00:00:00.0' AND mvt.WH_CODE = 'CL' AND mvt.BIN_CODE = 'PSADEPOT' )AS b ON a.SERIAL_NO = b.SERIAL_NO ) AND wm.MVT_DATE BETWEEN '2016-01-01 00:00:00.0' AND '2016-06-30 00:00:00.0' ORDER BY wml.SERIAL_NO, wml.MVT_DATE 传递给data函数(根据您的代码判断,我猜您假设{{1}实际上是对象)。

然后您提供insertData,显然是data,作为data.msg方法的第一个参数,期望您提供有效的对象这里。

另请注意,您在新创建的文档上使用undefined方法,而不是使用save

正如您在api documentation中所看到的,现在不推荐使用save。 (我猜您使用的是最新版本2.1)