简体   繁体   中英

Socket returns '[Object object]'

ive tried for hooours and cant find any solution, im trying to parse data to my server file which puts the content to a mysql database, but its always only outputting [object Object] and i seriously cant find any solution for this. Would appreciate any help.

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);
        });
    });
}

You want to use the val ue of firstname , not the object itself - so obtain this when you declare it:

var name1 = $('#firstname').val();

Do this for all your declarations to get the value.

The issue is with this

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

That returns a jQuery object - which is why the string value is [object object]

Try getting the value of the inputs (assuming they are inputs of course) using 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
    });  
});

alternatively, if you need the jQuery objects for some other reason

$(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()
    });  
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM