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?