0

I have written the following method that searches for a user in the database by their email.

/**
 * Find a user by providing their email address
 */
DataProvider.prototype.findUserByEmail = function(callback, email) {
console.log("in findUserByEmail");
User.findOne({
    emailAddress : email
}, function(error, user) {
    if(error) {
        console.log(error);
        callback(error);
    }
    else {
        console.log(user);
        callback(user);
    }
});
};

I'm trying to test it with the following:

function testFindUserByEmail() {
var expectedEmail = "[email protected]";
data.findUserByEmail(function(user) {
            if (user.emailAddress === expectedEmail) {
                console.log("User found");
            } else {
                console.log("User not found");
            }
    console.log(user);
}, "[email protected]");
console.log("test");
}

I get an outout of: in findUserByEmail test

It's like User.findOne() isn't getting called and I don't know why. Other info:

var UserSchema = new Schema({
emailAddress : {
    type : String
},
occupation : {
    type : String
},
token : {
    type : String
},
password : {
    type : String
},
registrationDate : {
    type : Date
},
activated : {
    type : Boolean
}
});

/**
 * Define Model
 */
var User = mongoose.model('User', UserSchema);
DataProvider = function() {
};

1 Answer 1

1

did you connected the database, try: mongoose.connect('db-uri', function (err) { next(err); });

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I had. I solved the issue though. Turns out there was a problem with the mongoDb connection. I was getting a 'Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84' error. Thanks for your answer - it turned me into the right direction of checking my connection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.