Sajeetharan
Sajeetharan

Reputation: 222522

Mongoose shows 0 results when data is there

I am using mongodb as my backend and it has around 400 documents inside the index _entity.

i am using mongoose with my nodejs and query it as follows,

 getCountries : function(event,context,callback){            
              dbHelper.query(mongoose.model('_entity'), {},function(error,data){
              console.log(data);
                callback(data);       
            });   

here is the dbHelper.

query : function(model, conditon,options) {
        return new Promise(function(resolve, reject) {
            options = options||{};               
            model.find(conditon, {}, options, function(error, data) {
                if (error)                  
                    reject(error);                    
                resolve(data);
            })
        })
    }

is there any issue with the code? when i run the query on mongo client, it shows the results.

enter image description here

Upvotes: 0

Views: 54

Answers (1)

Garuuk
Garuuk

Reputation: 2244

you're returning a promise in you query so you need to use the .then method instead of a callback.

 getCountries : function(event,context,callback){            
          dbHelper.query(mongoose.model('_entity'), {}).then(function(data){

           }); 
 }

edit: If you're using mongoose4 then you can just return a promise with the query itself like so.

query : function(model, conditon,options) {
            options = options||{};               
            return model.find(conditon, {}).exec();
    }

you'll still need to keep the .then with my previous response.

Upvotes: 1

Related Questions