Question on smart Relationships implementations

Hello :slight_smile:
i have some questions on the smart Relationships

I have Product document , each product have and embedded array of Object and i want to display them as a related Data .
I declared the model for my embeddedObjects
It raised a “Cannot read property ‘idField’ of undefined
at ResourceSerializer.perform” error
when trying to serialize it with Liana.ressourceSerializer
It works fine if my related Data is a colllection Document

I’m probably ( certainly :wink: ) doing something wrong but cannot figure it out :frowning:

Context

Please provide any relevant information about your setup.

  • Package Version: 5.7
  • Express Version: 4.16.3
  • Sequelize Version:
  • Database Dialect: mongodb js
  • Database Version:
  • Project Name: Panopli

Hello Tom,

Can you share with us the relevant configuration on your model, and some example data?

Can you also tell me if your objects are documents (meaning that they have a unique id in mongoDB), or if they are only plain objects not managed by mongodb as documents?

Thank you

Hello Guillaume :slight_smile:
My objects aren’t documents , they are only plain objects .

Here’s the model definition .

const mongoose = require('mongoose')

const schema = mongoose.Schema({
  templateCode: String,
  side: {
    type: String,
    enums: ['front', 'back', 'left', 'right'],
  },
  previewURL: String,
  id: String,
}, { _id: false })

module.exports = schema

exemple Product data =>

{
  "_id": {
    "$oid": "5f5a0cebeb17470a730b185b"
  },

  "embroideryInfos": [
    {
      "previewUrl": "",
      "templateCode": "Template 1",
      "side": "front"
    },
    {
      "previewUrl": "",
      "templatecode": "Template 3",
      "side": "back"
    },
  ],
}

and here the code implementation

const getEmbroiderys = async (req, res) => {
  try {
    const { productId } = req.params
    const product = await Products.findOne({ _id: ObjectID(productId) }).exec()
    const { embroideryInfos = [] } = product
    if (!embroideryInfos.length) {
    // do something
    }
    const embroidery = embroideryInfos.map((emb, index) => ({
      ...emb,
      id: `${productId}-${index}`,
    }))
    const serialized = await new Liana.ResourceSerializer(
      Liana,
      EmbroideryInfos,
      embroidery,
      null,
      { count: embroidery.length },
    ).perform()
    return res.status(200).send(serialized)
  } catch (e) {
    console.log('CATCHED', e)
    res.status(500).send(e)
  }
}

Hello,

Thanks a lot for all the details.

Forest Admin natively supports embedded arrays of plain objects in schema definition. You don’t need to declare a smart relationship for it to work.

For this feature to work, you have to declare your array inside the parent object (don’t declare a separate schema for the embedded structure)

// Product schema
const schema = mongoose.Schema({
  // [...]
  embroideryInfos: [{
    templateCode: String,
    side: {
      type: String,
      enums: ['front', 'back', 'left', 'right'],
    },
    previewURL: String,
    id: String,
  }]
})

Ty Guillaume , i removed the smart routes , and added the object definition in the Product model. But then the popup “Your admin panel is currently being updated. The changes will take place in a few seconds…” runs and never resolves. I can still see my previous smart relationship in the related data . But no sign of the new one as related Data .

Could you please share with me relevant parts of your model definition?

Before investigating further, can you restart your server and check again? There was an issue on our side, causing some issues with the update of settings.

The popup finally resolved :slight_smile: . I can see my related datas . Ty very much Guillaume

2 Likes