猫鼬推送在 socket.io 调用中不起作用
由于某种原因,推送到 mongodb 可以使用此设置,
// Fixed params
story.title = 'Socketi';
story.lines.push ({ author: 'Khuram', text:'socket data'});
//Capture data from socket into schema
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('slog', function (data) {
console.log(data);
});
});
但是将 mongoose 推送语句放在 socket.io 调用中不起作用,
如下所示:
// Fixed params
story.title = 'Socketi';
//Capture data from socket into schema
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('slog', function (data) {
story.lines.push ({ author: 'Khuram', text:data.my});
});
});
仅供参考:“slog”指的是我希望 mongoose 更新的事件。 我从客户端接收数据,并且我想在每次发出此事件时将套接字事件数据推送到 mongodb (这就是为什么我想将其放在 socket.io 调用中)
For some reason, the push to mongodb works with this setup
// Fixed params
story.title = 'Socketi';
story.lines.push ({ author: 'Khuram', text:'socket data'});
//Capture data from socket into schema
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('slog', function (data) {
console.log(data);
});
});
But placing the mongoose push statement within the socket.io call doesnt work
Like so:
// Fixed params
story.title = 'Socketi';
//Capture data from socket into schema
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('slog', function (data) {
story.lines.push ({ author: 'Khuram', text:data.my});
});
});
Just for reference purposes: 'slog' refers to the event that i want mongoose to update on.
Im receiving data from the client and i want to push the socket event data to mongodb each time this event is emitted (which is why i want to place it within the socket.io call)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的保存函数在哪里执行?我建议将您在回调中收到一条日志消息时希望发生的情况的所有逻辑放在其中,然后也在回调中调用故事实例的保存。
Where is your save function executing? I'd recommend putting all your logic for what you want to have happen when you receive a slog message in it's callback and then calling save on instance of story in your callback as well.