How to get DB value for a variable?

1

I have the following question: I have a database in mongoDB, I do the search of the data by NodeJS it returns me all right, however I would like to play this search value for a variable, to make a comparison. Type I search the date in DB and would like to compare that date with the current date of the system.

const CRUD = {
retrieve:function(query,mod){
    //console.log("Query",query);                   
    Model.findOne(query,mod, function(err,data){
        if(err) return console.log('ERRO: ',err);
        return console.log('Dado',data);                            
    });
};

Then I call this CRUD in another file. Is there a way to get the result of this search and play on one variable to compare with another? type like this:

var query3  = {data:'2016-12-04'};
var fields  = {data:1,_id:0};
CRUD.retrieve(query3,fields);

The result of CRUD is this: Given {date: '2016-12-04'}

    
asked by anonymous 04.12.2016 / 13:22

1 answer

1

You should create a new argument for this method, to have a callback that is run when the result is available.

const CRUD = {
    retrieve: function(query, mod, done){
        Model.findOne(query, mod, done);
    }
};

and then call this argument:

var query3  = {data:'2016-12-04'};
var fields  = {data:1, _id:0};
CRUD.retrieve(query3, fields, function(err, data) {
    if (err) return console.log('ERRO: ', err);
    // a partir daqui podes usar a variável "data"
    // e comparar o seu valor com outras variáveis
    console.log('Dado', data == 'algo a comparar');
});
    
04.12.2016 / 17:25