3

I am using Mongodb and the findOne feature:

User.findOne({$or: [{email: req.body.email}, {mobile: req.body.mobile}]}

However the issue I am facing is with req.body.email and req.body.mobile - in certain cases can be empty.

I have initially solved this using:

var toSearchSmartString = {$or: [{email: req.body.email}, {mobile: req.body.mobile}]};
if (req.body.email.length == 0) {
    toSearchSmartString = {mobile: req.body.mobile};
} else if (req.body.mobile.length == 0) {
    toSearchSmartString = {email: req.body.email};
}

then in the findOne, simply using:

User.findOne(toSearchSmartString);

So I want to check is this 'safe' todo? The reason I ask is this safe is because if I don't set the default value for toSearchSmartString and instead set it at the end of the if block (in a else) I get 'undefined' for the string.

I'm concerned that the findOne method may use the default toSearchSmartString before the if else condition has been checked? Am I right to concerned about this?

Alternatively is there some Mongodb function I can use to solve?

UPDATE: So after comments in answer below - having issues with the code:

I solved it by moving the var declaration above to where its used.

var contWithRegCallback = function(err, user) {
          console.log(user);
        }

    if (req.body.email.length == 0) {
          User.findOne({mobile: req.body.mobile}, contWithRegCallback);
        } else if (req.body.mobile.length == 0) {
          User.findOne({email: req.body.email}, contWithRegCallback);
        } else {
          User.findOne({$or: [{email: req.body.email}, {mobile: req.body.mobile}]}, contWithRegCallback);
        }

Namely the user in the callback function keeps returning undefined. Shouldnt it be the contents fro the fineOne?

halfer
  • 19,824
  • 17
  • 99
  • 186
userMod2
  • 8,312
  • 13
  • 63
  • 115

1 Answers1

4

Why don't you just use conditional check? This simple snippet should work as expected, notice, you want to filter by email or mobile.

var callback = function(err, result){
    if(err) {
        return res.status(400).send({message: 'Server error:' + JSON.stringify(err)});
    } else {
        res.json(result);
    }
}
if (req.body.email){
    User.findOne({email: req.body.email}, callback);    
} else if (req.body.mobile) {
    User.findOne({mobile: req.body.mobile}, callback);
} else {
    return res.status(400).send({message: "Email or Mobile required"});
}
Medet Tleukabiluly
  • 11,662
  • 3
  • 34
  • 69
  • Ah cool. That makes sense, but then in that call do i call the proceeding method: `var callback = new function() { console.log('continue from here'); }` If so then how do i get the result from the findOne? – userMod2 Feb 13 '16 at 13:47
  • Got it And also confirmed: http://stackoverflow.com/questions/9596276/how-to-explain-callbacks-in-plain-english-how-are-they-different-from-calling-o – userMod2 Feb 13 '16 at 13:51
  • Hi again, I've been playing around and have issues with getting the user - in my callback funtion when checking if the user is found (though console.log) i keep seeing undefined: – userMod2 Feb 13 '16 at 16:32
  • I solved it by moving the var decalrtion above to where its used. – userMod2 Feb 13 '16 at 16:46