0

I've started looking at socket.io and some MongoDB with mongojs.

Summary: Client doesn't send data to server through socket.io so mongojs doesn't have anything to look for and just returns first document in collection, I'd like it to return null or false so I can take appropriate action. Any help appreciated, very new to node.js and this.

I need a log in function, but when I click the link I've made to log in this should run from the client:

    <script>
        var socket = io.connect('http://localhost:13163');

        socket.on('connect', function () {
            socket.emit(console.log('Client connected'));
        });
        socket.on('queryResponse', function (res, err) {
            if (res == "[object Object]") {
                console.log('EMPTY OBJ SERVER: ' + res + ' ' + err);
            }
            else{
                console.log('IF NOT EXEC' + res[0].email)
            }
        });
        //socket.send('client hello');
        //pageload
        $(function () {
            $('#login').click(function () {
                var username = $('#username').val();
                var password = $('#password').val();
                if (!username || !password) {
                    event.preventDefault();
                    console.log('Need data in both fields');
                }
                else {
                    event.preventDefault();
                    socket.emit('querydata', function (username, password) {
                        console.log("CLIENT: Querying server for data...");
                    });
                }
            });
        });
    </script>

This should send the username and password to the server which should then run this:

socket.on('querydata', function (user_username, user_password) {
    db.users.find({email: user_username, pass: user_password}, function (err, docs) {
        if (err || !docs) {
            socket.emit('queryResponse', 'No users found', err);
            console.log('if - ' + docs[0].email);
        }
        else {
            socket.emit('queryResponse', docs);
            console.log('else - ' + docs[0].email + 'user_password' + user_password + 'user_username' + user_username);
        }
    });
});

What's weird about this is that, it finds only the first document of the collection. I've just found out that the 'user_username' and 'user_password' overloads are undefined, is this just some retarded mistake from my side?

Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
mackwerk
  • 1,689
  • 4
  • 21
  • 44

1 Answers1

2

You are doing it wrong. In a first look, i see couple of mistakes in your code. The first one here

if (res == "[object Object]") 

You can not control response content in that way. You probably see [object Object] string in your javascript debugger. You have to do this comparison according to the server response as a json data. If it will lead you a trouble you can use JSON.parse(response).

The second one, you do not send username and password to the server actually. You have to use

socket.emit('querydata', { username: username, password: password }, function (data) {
   // Server Ack here
}

instead of

socket.emit('querydata', function (username, password) ...

And try this

socket.emit('queryResponse', {err: 'No User Found'});

instad of

socket.emit('queryResponse', 'No User Found', err);

And in your client check the response as

if(response.hasOwnProperty('err')) / /Handle error

In you client side queryResponse listener, try this;

socket.on('queryResponse', function (res) {
        if(res.hasOwnProperty('err')) / Handle Error
        else // You have an array in `res`
    });
tolgaio
  • 3,206
  • 1
  • 19
  • 18
  • Thanks that helped on the server.js, still having trouble getting the data back to the client. I need to check if there is correct data in the document and if there is redirect and set a session, but if I try to use JSON.parse(docs) I'll get this error: Uncaught SyntaxError: Unexpected token u localhost:39 (anonymous function) localhost:39 EventEmitter.emit socket.io.js:633 SocketNamespace.onPacket socket.io.js:2248 Socket.onPacket socket.io.js:1930 Transport.onPacket socket.io.js:1332 Transport.onData socket.io.js:1303 stateChange – mackwerk Nov 15 '12 at 12:51
  • updated the answer, try updated part and let me know how it goes. – tolgaio Nov 15 '12 at 13:33
  • Well, I never get the err one because it always returns something for example if I enter uername: asd and password: asd this is what it returns: `xhr-polling writing 5:::{"name":"queryResponse","args":[{"email":"asd","pass":"asd"}]}` – mackwerk Nov 15 '12 at 13:42
  • This is correct response from server. It simply returns an array that contains 1 object init. This object has `email` and `pass` field. I think server response is correct. You should look at your client script which listens for event `queryResponse`. Try the update. – tolgaio Nov 15 '12 at 13:52
  • not quite sure if I understand what you mean by: `if(response.hasOwnProperty('err')) / /Handle error` what is the "response"? This is the listener right now: [JSFiddle](http://jsfiddle.net/MvKsb/) – mackwerk Nov 15 '12 at 14:00
  • I tried what you said you can check out the listener and the server.js emit here on [JSFiddle](http://jsfiddle.net/MvKsb/1/), with this setup I get an uncaught referenceError: users is not defined `(anonymous function) localhost:35` `EventEmitter.emit socket.io.js:633` `SocketNamespace.onPacket socket.io.js:2248` `Socket.onPacket socket.io.js:1930` `Transport.onPacket socket.io.js:1332` `Transport.onData socket.io.js:1303` `stateChange` I am definitely getting the wrong overload to the client because I can log out the correct info on the server. – mackwerk Nov 16 '12 at 08:25
  • Wow, good morning Kasper. I fixed it myself, I had written `if(users.hasOwnProperty)` should've wrote `if(res.hasOwnProperty)` - Thanks alot for helping! – mackwerk Nov 16 '12 at 08:45