How to get DB value for a variable?

Asked

Viewed 191 times

1

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

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 take the result of this search and play in a 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 : Data {date:'2016-12-04'}

1 answer

1

You should create a new argument for that 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 with that 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');
});

Browser other questions tagged

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