[Express] Smart Relationships do not display native belongsTo field

Hello

I am on the same contexte as [Express] Smart Relationship do not display global smart action

I have a 3 entity: Annonce, Application, Beneficiary
With these relations :

  • Annonce.hasMany(Application)
  • Annonce.BelongsTo(Beneficiary)

I created a Smart-relationships between Beneficiary and Application but the belongsTo link are not display. But if i click on a application record dans I back to my related data the link are well displayed.

Related data - Smart relationship (before record clicked)

Related data - Smart relationship (after record clicked)

Network response from Smart relationship

Network response Native related-data announce → application

In the native response we have the “relationships”. Is it a way to force response with the relationships (because i’m sure the problem from that)

collections/beneficiary.js

{
  field: 'Candidatures',
  type: ['String'],
  reference: 'application.id',
}

routes/beneficiary.js

router.get(
  '/beneficiary/:recordId/relationships/Candidatures',
  permissionMiddlewareCreator.details(),
  async (request, response) => {
    const limit = parseInt(request.query.page.size, 10) || 20;
    const offset = (parseInt(request.query.page.number, 10) - 1) * limit;

    const beneficiaryId = get(request, 'params.recordId');

    const { id: announceId } = await models.announce.findOne({
      where: { beneficiaryIdKey: beneficiaryId },
    });

    const { count, rows: applications } = await models.application.findAndCountAll({
      where: {
        announceIdKey: announceId,
      },
      offset,
      limit,
      order: [['createdAt', 'DESC']],
    });

    const serializer = new RecordSerializer(models.application);
    const recordSerialized = await serializer.serialize(applications, { count });

    return response.send(recordSerialized);
  },
);

Hello @Justin_Martin,

You have already posted the same topic. I’ve reminded @vince, he should reply to the original soon.

Thanks for your patience. :pray:

Kind regards,
Morgan

Yes it’s not the same error (juste same context)

Oh, sorry @Justin_Martin.

You have updated the topic in the meanwhile.

So to summarize, you don’t receive the right data when the data comes from the smart relationship custom route?

I think that the RecordSerializer is causing the issue. You can find more information here on how to serialize with an added belongsTo relationship.
Basically, the idea is to add some additional data to all records before the serialization occures.

// Add additionnal belongTo data
applications.forEach((candidature) => {
  candidature.annouce = { id: announceId };
  candidature.careGiver = { id: ??? }; // You need to fullfill the value
});

// Serialize your records
const recordSerialized = await serializer.serialize(applications, { count });

Let me know if it helps.

Kind regards,
Morgan

1 Like

Indeed I fix it by adding an include in my sequelize request :

  const { count, rows: applications } = await models.application.findAndCountAll({
    where: {
      announceIdKey: announceId,
    },
    offset,
    limit,
    order: [['createdAt', 'DESC']],
    include: ['announce', 'careGiver']
  });
1 Like

Glad you make it. :muscle: Have a nice day.

Kind regards,
Morgan