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.
– Caputo
Can you comment here on the part you’re referring to? Which framework? Sorry, I’m new to this.
– debeka
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..
– debeka
And I really got lost in what you wrote. "The problem is the key quotes", if you try with
{'sessionIDtype': sessionID}
what happens?– Caputo
@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.
– debeka
And if you try:
var condicao = new Object();
and thencondicao[param1] = value;
and finallysessions.findOne(condicao)
– Caputo