Models enrichment/relationship doesn't seem to work as expected

I am currently trying to display the documents of a MongoDB database with their relations. As of now, I have the following:

User(s) -> [Has outfit(s)] -> Outfit(s) -> [List of item(s)/product(s)] -> Product(s)

We will, for the sake of this example, take only the Outfit model and the Product model. Here you can find two documents as a possible example (JSON Format):

// Outfit document
{
    "_id" : ObjectId("5eeb712dfa3a7b9028787903"),
    "picture_url" : "https://i.skyrock.net/3911/84063911/pics/3226492571_1_2_q8PvGEGD.jpg",
    "products_ids" : [ 
        ObjectId("5eeb712dfa3a7b9028787904"), 
        ObjectId("5eeb712dfa3a7b9028787905"), 
        ObjectId("5eeb712dfa3a7b9028787906"), 
        ObjectId("5eeb712dfa3a7b9028787907")
    ]
}

// Outfit Product Document
{
    "_id" : ObjectId("5eeb712dfa3a7b9028787905"),
    "outfit_id" : ObjectId("5eeb712dfa3a7b9028787903"),
    "title" : "Chemise bucheron, rouge/noir, a carreaux",
    "picture_url" : "https://www.projet13.com/9959-big_default/chemise-bucheron-rouge-noir-a-carreaux.jpg",
    "price" : NumberLong(4200),
    "redirect_url" : "https://www.projet13.com/6426-chemise-bucheron-rouge-noir-a-carreaux.html?gclid=Cj0KCQjwoaz3BRDnARIsAF1RfLfOJDZobj5gTT_uscskeyUqWgzZ_L2SJYvE_kMmk4weuzZpe-fc8cQaAr-IEALw_wcB#/10-taille-m/32-couleur-rouge"
}

Now, if we look at the forest admin part (model definitions), we have the following:

// Outfit Document
// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
const mongoose = require('mongoose');

// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = mongoose.Schema({ 
  'picture_url': String,
  'products': [{ type: mongoose.Schema.Types.ObjectId, ref: 'outfitProductsCollection' }],
}, {
  timestamps: false,
});

module.exports = mongoose.model('outfitsCollection', schema, 'OutfitsCollection');
// Outfit Product Document
// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
const mongoose = require('mongoose');

// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = mongoose.Schema({ 
  'outfit_id': { type: mongoose.Schema.Types.ObjectId, ref: 'outfitsCollection' },
  'title': String,
  'price': Number,
  'redirect_url': String,
  'picture_url': String,
}, {
  timestamps: false,
});

module.exports = mongoose.model('outfitProductsCollection', schema, 'OutfitProductsCollection');

Expected behavior

Now that I have my belongsTo and hasMany setup, I do have the following behavior (see below images)

Outfit details

I have the details with a “link” to Products (which contains the related products)

Outfit Product details

I have the details of my product but not “link” to the related outfit

Actual behavior

I can’t retrieve the related document as it should.

Documentation links:

Failure Logs

I have no failure logs at all.

Context

Please provide any relevant information about your setup.

  • Project Name: EyesApp
"dependencies": {
    "chalk": "~1.1.3",
    "cookie-parser": "1.4.4",
    "cors": "2.8.5",
    "debug": "~4.0.1",
    "dotenv": "~6.1.0",
    "express": "~4.16.3",
    "express-jwt": "5.3.1",
    "forest-express-mongoose": "^6.0.0",
    "mongoose": "~5.8.2",
    "morgan": "1.9.1",
    "npm-watch": "^0.6.0",
    "require-all": "^3.0.0"
  }

We are currently plugged to MongoDB, no firebase implemented yet.

Hi @Thomas

I’ll try to reproduce your issue today and will let you know if I find something.

Unfortunately, I cannot manage to reproduce this issue.

I tried with this model:

const schema = mongoose.Schema({
  'children': { type: [mongoose.Schema.Types.ObjectId], ref: 'persons' },
  'parent': { type: mongoose.Schema.Types.ObjectId, ref: 'persons' },
  'name': String,
}, {
  timestamps: false,
});

module.exports = mongoose.model('persons', schema, 'persons');

And everything is well set and displayed.

I am using the same version of mongoose and forest-express-mongoose as you do.
What is the version of your Mongodb database?

Hi,

Thanks for your time, we are using MongoDB on Atlas on a Version 4.2.6 Cluster

Best,

Hi Thomas,

I jumped to your usecase this morning and tried to reproduce your issue with no success.

However, I notice one little thing that could make the difference.
Can you please try to replace, in your outfits-collection model, the key products by products_ids?
Your model would be like this:

const mongoose = require('mongoose');

const schema = mongoose.Schema({
  'picture_url': String,
  'products_ids': { type: [mongoose.Schema.Types.ObjectId], ref: 'outfitProductsCollection' },
}, {
  timestamps: false,
});

module.exports = mongoose.model('outfitsCollection', schema, 'OutfitsCollection');

Let me know if that changes anything.
Thanks!

PS: Really love the outfit example you gave us, the “Chemise bucheron” is our uniform :bearded_person: :evergreen_tree: :pick:

Hey,

While checking, I found something weird

I had this line:

'products': [{ type: mongoose.Schema.Types.ObjectId, ref: 'outfitProductsCollection' }],

However, you actually did something different:

'products_ids': { type: [mongoose.Schema.Types.ObjectId], ref: 'outfitProductsCollection' },

As you can notice, you did define {type: [ … ]} and not [{type: …}]. As per the documentation -> https://docs.forestadmin.com/documentation/reference-guide/relationships#adding-a-hasmany-relationship I did good?

But as usual… the link still don’t work (even if I was wrong because it was product_ids and not products)


Ok so, from the outfit, I can now finally access the related products!!

const schema = mongoose.Schema({ 
  'picture_url': String,
  'products_ids': [{ type: mongoose.Schema.Types.ObjectId, ref: 'outfitProductsCollection' }],
}, {
  timestamps: false,
});

However, in the reversed way, I only get a string/data like this:

Outfit id
<client@model:forest-outfits-collection::ember776:5eff238d2935aec34c3b961e>

Ok I don’t get it… I updated the models section

from:

// outfits-collection.js
module.exports = mongoose.model('outfitsCollection', schema, 'OutfitsCollection');

// outfit-products-collection.js
module.exports = mongoose.model('outfitProductsCollection', schema, 'OutfitProductsCollection');

to:

// outfits-collection.js
module.exports = mongoose.model('OutfitsCollection', schema, 'OutfitsCollection');

// outfit-products-collection.js
module.exports = mongoose.model('OutfitProductsCollection', schema, 'OutfitProductsCollection');

And now I do have the expected behavior, wut…