var io = require('socket.io').listen(8888,"myIp");
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/troll1');
var db = mongoose.connection;
io.sockets.on('connection', function (socket) {
socket.on('registerForm', function (data) {
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
socket.emit('registerFormConfirm',{"ok":1});
});
});
});
好的,那个代码在我的server.js中。由于某种原因,发射不起作用,下面的代码正常工作:
var io = require('socket.io').listen(8888,"myIp");
io.sockets.on('connection', function (socket) {
socket.on('registerForm', function (data) {
socket.emit('registerFormConfirm',{"ok":1});
});
});
我和其他软件包一样安装了mongoose normaly。不知道我能在终端上做些什么来检查它是否正确
答案 0 :(得分:0)
mongoose在调用open
时发出mongoose.connect
事件,但您的事件侦听器设置得太晚,无法捕获该事件。
将代码修改为
var io = require('socket.io').listen(8888,"myIp");
var mongoose = require('mongoose');
var db = mongoose.connection;
io.sockets.on('connection', function (socket) {
socket.on('registerForm', function (data) {
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
socket.emit('registerFormConfirm',{"ok":1});
});
mongoose.connect('mongodb://localhost/troll1'); // connect here
});
});