Return an empty Mongoose array

I try to use the mongoose fill function, but in response I get an empty array, I saw several messages about this

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);

This is my schema definition for the base model.

var mongoose = require ('mongoose');

var PackageSchema = new mongoose.Schema({
    merchant_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Merchant' },
    name: String,
    original_price: Number,
    discounted_price: Number
});

module.exports = mongoose.model('Package', PackageSchema);

And this is the model I'm talking about. The data inside the package model and the Merchant model is saved just fine.

Sales document

Package document

But if I request the use of the populate function, an empty string is returned to me

Merchant
.findById( req.params.id, 'packages')
.populate('packages')
.exec(function (err, merchant) {
    if (err)
        next(err);
    else
        res.status(200).json(merchant);
});

Conclusion:

{
  "_id": "579b3b2dc2e8d61c0ecd2731",
  "packages": []
}

Can anyone help me

Update:

Well, something really strange is happening. If I try to fill out a package document with merchant_id, it works, but not vice versa.

Package
    .find()
    .populate('merchant_id')
    .exec(function (err, packages) {
        if(err)
            next(err);
        else
            res.status(200).json(packages);
    });

Conclusion:

[
  {
    "_id": "579b3b51c2e8d61c0ecd2732",
    "name": "Hair + Nails",
    "original_price": 600,
    "discounted_price": 400,
    "merchant_id": {
      "_id": "579b3b2dc2e8d61c0ecd2731",
      "updated_at": "2016-07-29T11:17:37.474Z",
      "created_at": "2016-07-29T11:17:01.216Z",
      "name": "VLCC",
      "logo_image": "http://vlccwellness.com/India/wp-content/uploads/2015/09/logo1.png?",
      "cover_image": "http://image3.mouthshut.com/images/imagesp/925053993s.jpg?",
      "__v": 1,
      "tags": [],
      "packages": [
        "579b3b51c2e8d61c0ecd2732"
      ],
      "work_hours": {
        "opening_time": 1000,
        "closing_time": 2100,
        "holiday": "Sunday"
      },
      "information": {
        "description": "Lorem Ipsum",
        "gender": "Men",
        "services": [
          "Hair"
        ],
0
source share
4

insted $type 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);

, ObjectId Merchant.

+1

findBbyId:

Merchant
.findById( req.params.id)
.populate('packages')
.exec(function (err, merchant) {
    if (err)
        next(err);
    else
        res.status(200).json(merchant);
});
0

Good, because your field packageson yours Schemais an array that you should fill as an array.

Try

.populate([
    {path:'packages', model:Package}
])

wher Packageis an instance of your package model.

0
source

Ensure that the package array in the Merchant schema contains ObjectIds strings of type (not number). You can provide this with something like:

merchant.packages.map(r => { r._id = r._id + ''; });
0
source

Source: https://habr.com/ru/post/1683149/


All Articles