Wednesday, 3 May 2017

Mongoose populate with 2 Files and 2 Collections return empty array

I have the problem that my populate query returns an empty array. I am aware of the countless posts, however none of them provided new insights for me.

My first Schema: station.js

var stationSchema = new Schema({ 
    _id: Number, 
    Tripcount: [{ type: Number, ref: 'Tripcount'}]
},
    {collection: 'stations'}
);
module.exports = mongoose.model('Station', stationSchema);

My second: trips.js

var tripSchema = new Schema({
    _id: Number,
    Tripcount: Number
},
    {collection: 'trips'}
);
module.exports = mongoose.model('Tripcount', tripSchema);

And my query looks like this, according to the majority of the answers this should do the job:

var Station = require('./station.js');
var query = Station.find()
        .populate({
            path: 'Tripcount',
            model : 'Tripcount'
        });
        query.exec(function(err, stations){
            if(err)
                res.send(err);
            res.json(stations);
        });

Using Postman to GET the stations, it shows Tripcount: []. Any ideas what I am doing wrong?



via ffritz

No comments:

Post a Comment