[Headless] Relationships showing as array of ids instead of value

  • Hi,


    I've just started playing around with headless feature of Runway.


    I have two collection: `authors` and `news` – for which I have configure headless access. News template has a relationship with `authors` collection so that content editor can assign an author for a given news.


    I do get the json response, but the author is returned as an array of what seems to be `ids`.


    Code: author.html
    1. <perch:content type="text" title="true" id="firstname" label="First name">
    2. <perch:content type="text" title="true" id="lastname" label="Last name">
    Code: news.html
    1. <perch:content id="heading" type="text" label="Heading" required title>
    2. <perch:related id="author" collection="Authors">
    3. <p>
    4. <perch:content type="text" id="firstname">
    5. <perch:content type="text" id="lastname">
    6. </p>
    7. </perch:related>
    Code: news.json
    1. {"status":"ok","sets":{"news":[{"_id":"13","heading":"Test","_title":"Test","date":"2020-01-15","author":["10","11"],"_sortvalue":"2020-01-15"},{"_id":"14","heading":"Test2","_title":"Test2","date":"2020-01-15","author":["10"],"_sortvalue":"2020-01-15"}]}}

    Any idea on how to get the relationship to be returned as part of news.json?


    Thanks,

  • drewm

    Approved the thread.
  • is It possible you added the title attribute to the author after the content was created? If so you’ll need to re-save them. I literally had this happen 20min ago.

    You are right, I did add the title attribute afterwards but the issue remains even after I delete the two collections and create them again :-/

  • This is how related items data is fetched. If you need the related item's data, you'll need to fetch that separately.


    perch_collection() behaves the same way. However, inside a template the perch:related tags fetches the related items (and renders the content) for you. When you add fields inside a perch:related these are only used for output. It makes no difference to how Perch saves the data.


    So you'd use something like this:


  • Hi, and thanks for the example!


    Here you get two different sets. What would you do to merge the (resolved) authors directly into the news set?


    * Plug into the for each loop, render author data for each post - how to add that to the set?

    * Create two sets first and merge them manually?

    * Stick with the two sets and resolve the relation in the client, not in the data?


    Thanks!