【发布时间】:2014-02-19 09:27:02
【问题描述】:
每当发出套接字时,我都会尝试使用 nodejs 在我的 mongodb 中插入一些数据。代码如下:
io.sockets.on( "connection",function( socket ){
socket.on( "send", function( data ) {
console.log(data.name + " and the content is: " + data.content);
mongodb.connect( "mongodb://127.0.0.1", function( err, db ) {
if(err) throw err;
var to_be_inserted = {name: data.name,content: data.content};
db.collection("chat").insert(to_be_inserted,function(err,objects){
if(err) throw err;
});
})
})
})
但是每当我去我的 mongo 控制台并输入
db.chat.find()
我找不到插入的记录。我确定我已经打开了 mongod,并且我确定套接字已发出。此外,插入之前的 consoloe.log 确实有效。
这是我的 mongo 客户端
var mongodb = require("mongodb").MongoClient;
我的运行 nodejs 服务器的控制台没有记录任何错误。
【问题讨论】:
-
在
db.collection("chat").insert callback内尝试立即获取相同的数据。在那之后,我们可能会得到更多的思考。 -
因此,如果这不起作用“console.log(data.name +”并且内容为:“+ data.content);”这告诉你什么?
-
他说:...**确实**有效。 ;-)