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

Asked

Viewed 333 times

1

I am trying to recover 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 within it. And I’m just trying to print the document recovered from 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)
    }); 
}

It’s returning this at the terminal:

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

and never gets into this if:

if(sessionDoc != undefined)

I think the error is in the query, but I’ve tried it:

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 Documentation of Mongoose: source :http://mongoosejs.com/docs/queries.html

The problem is not the quotes in the value :

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

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

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

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

sessions.findOne({'parametro1': value}

I can’t do it above. It wouldn’t work.

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

funcao(param1, value){

 if(param1 == "googleUID")

sessions.findOne({'googleUID': value}

} //Isso funcionaria

But what if I have 10 possibilities of identifiers? 10 if’s not from!

then, how to quote on the identifier?

  • tried it without quotation marks in sessionid ? Usually the data framework takes care of it for you.

  • Can you comment here on the part you’re referring to? Which framework? Sorry, I’m new to this.

  • You’re right. That would work: Session.findOne({'key':variable}) without quotation marks on the value, but the problem is the key I don’t know what it is..

  • And I really got lost in what you wrote. "The problem is the key quotes", if you try with {'sessionIDtype': sessionID} what happens?

  • @Caputo , you sure would work. But the key I have in the document n is 'sessionIDtype', sessionIDtype is the variable that stores the name of my key.It can be 3 different names. I can do 3 ifs, but what if I had 10 different names? that’s the problem. Instead of repeating the code 3 times I wanted to do only once.

  • And if you try: var condicao = new Object(); and then condicao[param1] = value; and finally sessions.findOne(condicao)

Show 1 more comment

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){
        // ...
    }
}

0

This here is gonna work:

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

Editing after your comment:

How about you do it 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){
  • It doesn’t work. It would only work like this with quotes : Sessions.findOne({'key': sessionid}). The problem is q the key word can be 3 different words. Like "fbUID," "googleID," "twitterID"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.