Query using Mongoose in Nodejs always returns null even within asynchronous method

1

I'm trying to retrieve a document in my Database using Mongoose in Node.js but it always returns Null. My problem before was the asynchronous method, but now everything is running inside it. And I'm just trying to print the recovered document from the Database, but it always returns this:

#session from the database ++++++++++++++++ null

This is my job:

function isAuthorized(sessionID, sessionIDtype, callback){
    var id = "'"+ sessionID+"'"
    sessions.findOne({'sessionIDtype': id}, function(err, sessionDoc){
        if(err) return handleError(err);
        if(sessionDoc != undefined){ 
            log.d("session from the database inside no if != undefined++++++++++++++++", sessionDoc)
            if(sessionDoc.date < new Date()){ //fix validation on data, expires needs to be < new Date
                callback(sessionDoc);
            }else{
                callback(undefined);
            }
        }else{
            callback(undefined)
        }
        log.d("session from the database ++++++++++++++++", sessionDoc)
    }); 
}

You are returning this in the terminal:

#session from the database ++++++++++++++++ null

and never enter this if:

if(sessionDoc != undefined)

I think the error is in the query, but I've tried this:

sessions.findOne({'sessionIDtype': sessionID}, function(err, sessionDoc){

and this:

var query = {}
query["'"+ sessionIDtype +"'" ] = sessionID;
sessions.findOne(query, function(err, sessionDoc){

but if I replace the values the query is executed. With this code it works:

 sessions.findOne({'googleUID': '12314'}, function(err, sessionDoc){

according to the Mongoose Documentation: source: link

The problem is not the quotation marks in the value:

 sessions.findOne({'key': 'value'}

The problem is the key quotes: for the query to work the first word has to be enclosed in quotation marks, but the value does not need this. Exem:

sessions.findOne({'key': value} //Isso funcionaria, eu ja testei. 

The problem is that I get the name of the identifier and the value by the above function. So, I can only replace the value with the parameter I get, but the handle does not. Exem:

sessions.findOne({'parametro1': value}

I can not do this above. It would not work.

I can create an if for each case, but the code would look pretty ugly. Exem:

funcao(param1, value){

 if(param1 == "googleUID")

sessions.findOne({'googleUID': value}

} //Isso funcionaria

But what if I have 10 possibilities for identifiers? 10 if's not the case!

So, how do you put quotation marks around the identifier?

    
asked by anonymous 23.07.2014 / 17:01

2 answers

1

Create the object with the parameters first, and use the bracket notation to have a dynamic key:

function isAuthorized(sessionID, sessionIDtype, callback){
    var parametros = {};
    parametros[sessionIDtype] = sessionID;
    sessions.findOne(parametros, function(err, sessionDoc){
        // ...
    }
}
    
24.07.2014 / 18:59
0

This will work here:

sessions.findOne({sessionIDtype: sessionID}, function(err, sessionDoc){
   ...
});

Editing after your comment:

How about doing this then:

var criteria;

if (...) {
  criteria = {
    fbUID: '1234'
  };
} elsif (...) {
  criteria = {
    googleID: '4567'
  };
else {
  criteria = {
    twitterID: '7890'
  };
};

...

auth = isAuthorized(criteria, cb);

...
function isAuthorized(queryCriteria, callback){
    sessions.findOne(queryCriteria, function(err, sessionDoc){
    
24.07.2014 / 14:35