gpt4 book ai didi

mysql - Socket.io MySQL session

转载 作者:行者123 更新时间:2023-11-29 23:26:06 24 4
gpt4 key购买 nike

如何将 mysql insertId 存储并访问到 socket.io session 中?

var mysql = require('mysql');
var express = require('express');
var app = express();
var router = express.Router();
var https = require('https').createServer( ssl_options, app);
var io = require('socket.io')( https );

io.on('connection', function(socket){

socket.on('new user', function(data) {

socket.username = data.username;

var query = connection.query('INSERT INTO chat SET ?', {subject:'1',message:'2'}, function(err, result) {

socket.chat_id = result.insertId;

});

console.log ( socket.username ) // Username
console.log ( socket.chat_id ) // Nothing returns?

});

});

最佳答案

我怀疑您的插入是否有效,因此它实际上没有插入值。我认为您想将插入内容更改为类似的内容。另外,添加错误测试

var query = connection.query('INSERT INTO chat (subject, message) VALUES (?, ?)', {subject:'1',message:'2'}, function(err, result) {
if(err) {
console.log(err.message);
return;
}
socket.chat_id = result.insertId;
});

关于mysql - Socket.io MySQL session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26948031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com