0

Now I've played with nodeJS and SocketIO and all went fine. But now I get caught at one query!

// User Auth Event
socket.on('userAuth', function(userObj) {

     var queryUserAuth = User.find({
         name : userObj.name
     })
     .where('password',userObj.pword);
     queryUserAuth.exec(function(err, userData){

        if(err) {
            socket.emit('userAuthOK',err);
            console.log('!! User: %s or PW not OK', userObj.name);
            return handleError(err);
        }else {
            console.log('User: %s known, userID: %s', userObj.name, userData.userid);
            socket.emit('userAuthOK', userData.userid);
            socket.join(userData.userid); // Create new room / join this room
        }
     });

});

But all the time the var "userData" is empty and the console.log above tells me "User: testuser known, userID: undefined". I can't get this user object from MongoDB this ways, but I can find the user if I take the mongo console.

/////////// EDIT: After some changes... here the new code... but still without finding the right record and getting a result of null.

// User Auth Event
socket.on('userAuth', function(userObj) {
     var queryUserAuth = User.findOne({
         name : userObj.name,
         password : userObj.pword
     });
         //.where("password").equals(userObj.pword);
    queryUserAuth.exec(function(err, userData){

        if(err) {
            socket.emit('userAuthOK',err);
            console.log('!! User Auth Error: %s', err);
            return handleError(err);
        }else {
            if(!userData){
                console.log('!! User: %s or PW not OK.', userObj.name);
                socket.emit('userAuthOK','fail');
            }else {
                console.log('User: %s known, userID: %s', userObj.name, userData);
                socket.emit('userAuthOK', userData);
                socket.join(userData.userid); // Create new room / join this room
            }
        }
     });

});

Here the output by manual query on mongo shell:

db.user.find({name: 'testuser', password: 'test'}) { "_id" : ObjectId("55a8cc8240fdc97f108d4d11"), "userid" : "1", "name" : "testuser", "email" : "test@test.com", "password" : "test" }

And this is the value of userObj: { name: 'testuser', pword: 'test' }

///// EDIT2:

here the user.js including the mongoose model of User:

// Load the MongoDB module
var mongoose = require('mongoose');
// user schema
var userSchema = mongoose.Schema({
    userid: Number,
    name: String,
    email: String,
    password: String,
    status: Number
});


// compiling the user schema
var User = mongoose.model('User', userSchema);

// make this available to our users in our Node applications
module.exports = User;

Here the include of the model:

// Load Mongoose models
var User = require('./models/user');
km65
  • 21
  • 9

1 Answers1

0

Try logging userData instead of just userData.userid. It's an Array, not an Object. You want to use .findOne(), not .find().

Also, the syntax for the query methods is:

.where("password").equals(userObj.pword)

Or, you could just add it to your original .findOne():

var queryUserAuth = User.findOne({
    name: userObj.name,
    password: userObj.pword
    });

In addition, your console.log('!! User: %s or PW not OK', userObj.name); doesn't make any sense. Not finding anyone is different than getting an error. You'll want to handle that part as a separate if statement nested within the else block.

chrisbajorin
  • 5,993
  • 3
  • 21
  • 34
  • Thanks for your reply. I've tried this way, too with result of NULL... userObj doesn't show anything, but NULL. – km65 Jul 19 '15 at 19:10
  • Your mongoose syntax is correct excluding the `.find()`. EDIT: I misread it, it should be `.where("password").equals(userObj.pword)` – chrisbajorin Jul 19 '15 at 19:14
  • @km65 I'll update my answer – chrisbajorin Jul 19 '15 at 19:18
  • I've changed the query but still null. :-( – km65 Jul 19 '15 at 19:25
  • @km65 can you add `userObj` as well as mongo shell document you are finding to your post? – chrisbajorin Jul 19 '15 at 19:26
  • I don't know if I get the right you need... but this is the mongo shell output by manual command: > db.user.find({name: 'testuser', password: 'test'}) { "_id" : ObjectId("55a8cc8240fdc97f108d4d11"), "userid" : "1", "name" : "testuser", "email" : "test@test.com", "password" : "test" } And this is the userObj: { name: 'testuser', pword: 'test' } – km65 Jul 19 '15 at 19:32