套接字返回“ [Object object]”

时间:2019-04-25 22:52:27

标签: javascript jquery html websocket socket.io

我试图解决问题,找不到任何解决方案,我试图将数据解析到我的服务器文件中,该文件将内容存储到mysql数据库中,但是它始终只输出[object Object],我严重无法为此找到任何解决方案。不胜感激。

Main.js

   "use strict";
$(document).ready(function() {
    var socket = io.connect(":2096");
        var s = socket;
        function connect() {
                if (!socket) {
                    if (hash == "" || hash != "") {}
            socket = io(":2096");
            s.on('connect', function() {
                if (hash != "") {}
                s.emit('hash', hash);
            });
        }
}
    connect();
});
$(document).on('click', '.adm-req', function(data) {
    var name1 = $('#firstname');
    var lastname = $('#lastname');
    var userid = $('#userid');
    var pin = $('#pin');
    var password = $('#password');
    socket.emit('--get-request_adm', {
        firstname: name1,
        lastname: lastname,
        userid: userid,
        pin: pin,
        password: password
    });  
});

Server.js

var mysql = require('mysql');
var log4js = require('log4js');
var request = require('request');
var fs = require('fs');
var crypto = require('crypto');
var md5 = require('md5');
var sha256 = require('sha256');
var math = require('mathjs');
var logger = log4js.getLogger();
var nodemailer = require('nodemailer');


var express = require('express');
var app = express();

var httpServer = require('https').createServer(options, app)
var io = require('socket.io')(httpServer);
httpServer.listen(2096);

io.on('connection', function(socket) {  
    socket.on('--get-request_adm', function(data) {
        pool.query('INSERT INTO `users` SET `firstname` = "'+data.firstname+'"')
    });
});
function query(sql, callback) {
    if (typeof callback === 'undefined') {
        callback = function() {};
    }
    pool.getConnection(function(err, connection) {
        if (err) return callback(err);
        connection.query(sql, function(err, rows) {
            if (err) return callback(err);
            connection.release();
            return callback(null, rows);
        });
    });
}

2 个答案:

答案 0 :(得分:0)

您要使用val的{​​{1}} ue,而不是对象本身-因此在声明它时要使用它:

firstname

对所有声明执行此操作以获取值。

答案 1 :(得分:0)

问题出在此

var name1 = $('#firstname');
// etc

这将返回一个jQuery 对象-这就是字符串值为[object object]

的原因

尝试使用jquery.val()获取输入的(假设它们当然是输入)

$(document).on('click', '.adm-req', function(data) {
    var name1 = $('#firstname').val();
    var lastname = $('#lastname').val();
    var userid = $('#userid').val();
    var pin = $('#pin').val();
    var password = $('#password').val();
    socket.emit('--get-request_adm', {
        firstname: name1,
        lastname: lastname,
        userid: userid,
        pin: pin,
        password: password
    });  
});

或者,如果由于其他原因需要jQuery对象

$(document).on('click', '.adm-req', function(data) {
    var name1 = $('#firstname');
    var lastname = $('#lastname');
    var userid = $('#userid');
    var pin = $('#pin');
    var password = $('#password');
    socket.emit('--get-request_adm', {
        firstname: name1.val(),
        lastname: lastname.val(),
        userid: userid.val(),
        pin: pin.val(),
        password: password.val()
    });  
});