Mongoose populate returning empty array

后端 未结 4 1041
我寻月下人不归
我寻月下人不归 2021-01-28 04:25

I am trying to use mongoose populate function but in response I am getting empty array, I have seen multiple posts regarding this

var MerchantSchema = new mongo         


        
4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-28 04:41

    Use type insted of $type in MerchantSchema.

    var MerchantSchema = new mongoose.Schema({
      packages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Package'}]
    },
    {
        typeKey: '$type',
        timestamps: { createdAt: 'created_at', updatedAt: 'updated_at'}
    });
    
    module.exports = mongoose.model('Merchant', MerchantSchema);
    

    Verify there is an array of ObjectId against packages in your Merchant document.

提交回复
热议问题