Jamie Hutber
Jamie Hutber

Reputation: 28126

Return results mongoose v4 with find query

I have left my code for over a year and have ran NPM install and since it obviously has changed :)

My Query

var mongoose = require('mongoose'),
    Clubs = mongoose.model('Clubs');

getNorsemanClubs: function(passData){
    return new Promise(function (resolve) {
        Clubs.find({norseman:true}, 'name teamID date norseman eleven').sort({ eleven : 'asc'})
            .then(function(clubs){
                console.info(clubs);
                passData.clubs = clubs;
                resolve(passData);
            });
    })
}

console.info(clubs); Output

enter image description here

Here you can see that it is returning the model rather than the results of the clubs.

My model

var mongoose = require('mongoose')
    , Schema = mongoose.Schema;

/**
 * User Schema
 */

var ClubsScheme = new Schema({
    name: String,
    teamID: { type: String, default: null },
    date: { type: Date, default: Date.now},
    norseman: {type: Boolean, default: false},
    eleven: Number
})

mongoose.model('Clubs', ClubsScheme);

How can I get it return a list of the clubs?

The clubs table is full of match data. But I used to get a return from 'name teamID date norseman eleven' now I just get a return of :\

**Using exec()

enter image description here

Upvotes: 0

Views: 49

Answers (1)

Basim Hennawi
Basim Hennawi

Reputation: 2721

You need to execute your find mongoose query method, by trailing your query with exec() method so that it return a Promise with your list values resolved, so change the following line:

Clubs.find({norseman:true}, 'name teamID date norseman eleven').sort({ eleven : 'asc'})

to:

Clubs.find({norseman:true}, 'name teamID date norseman eleven').sort({ eleven : 'asc'}).exec()

Upvotes: 1

Related Questions