Get related objects for every row in query results

Hello, I have a query which finds an Audit, then some related DetailAudit objects (one-to-many) and this related model has a “relation” column standards (many-to-many).

The DetailAudit objects are passed to a view and in that view I have to get the standards related objects but I don’t know how.

How can I extract the “standards” relation column in my pug view?

This is the function that gets these objects:

function show(id, handler) {
  var query = new Parse.Query(Audit);
  query.include("auditTest");
  query.include("standar");
  query.include("category");
  query.include("type");
  query.include("auditLeader");
  query.include("auditTest");
  query.include("conclusiones");
  query
    .get(id)
    .then(function (audit) {
      var query = new Parse.Query(DetailAudit);
      query.equalTo("audit", audit);
      query.include("audit");
      query.include("auditor");
      query.include("standar");
      query.include("standards");
      query.include("area");
      query.include("process");
      query.limit(1000);
      query.find({
        success: function (details) {
          audit.details = details;
          handler({
            status: 200,
            msn: "Detalles listadas exitosamente",
            audit: audit,
          });
        },
        error: function (error) {
          //handle error
        },
      });
    })
    .catch(function (error) {
      // catch error
    });
}

Thanks in advance.

It should be something like obj.relation('relationName').query.find()

You can see more details here: JavaScript Developers Guide | Parse

Sidenote, the JS API for include accepts an array as well, so rather than include('a').include('b').include('c'), you can just do include(['a', 'b', 'c']).

1 Like

Thanks for your response.

I can get the related objects as you mention but only when querying ID of a record. What I have is a list of DetailAudit which are passed to a view. In that view I am looping through the array but I don’t know how to get the related “standards” detailed in an array.

Is there any way to loop through the “standards” object to build an array and then assign that array to each DetailAudit as a new property?

I am really lost here. I just want to get that related objects as an array or a comma separated string.

I believe you can retrieve all the data you need in a single query, which is more efficient. But I believe it is also technically possible to loop through the array and get the related objects. Give it a try and let me know if you need help.