Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Referencing related metaobjects

Solved

Referencing related metaobjects

theevilgenius
Shopify Partner
4 0 1

I cannot reference metaobjects that are referenced from metaobjects.

 

The metaobjects are set up properly and I can see the gid values for the objects in the reference field, but I can't actually do anything with the referenced objects.


This is the proof that the metaobject schema is populated properly.

See here that there are categories and they have event metaobjects referenced in the events field.

Console Output:

Raw Categories Data:

  1.  
    (2) [{…}, {…}]
    1. 0:
      1. description: "Lorem ipsum dolor sit amet."
      2. events: Array(2)
        1. 0: "gid://shopify/Metaobject/101002641431"
        2. 1: "gid://shopify/Metaobject/101002706967"
        3. length: 2

 

Now take a look at this part of the script. The goal is to be able to iterate through these events and then use them in the code.

<script>
  {% for category in shop.metaobjects.event_category.values %}
    console.log('Category ID: {{ category.system.id }}'); <!-- This outputs the category ID -->
    console.log('Category Name: {{ category.name }}'); <!-- This outputs the category name -->

    {% if category.events %}
      console.log('Category {{ category.system.id }} Events Field Exists'); <!-- This outputs the that the fields exist -->
      console.log('Category {{ category.system.id }} Events: {{ category.events }}');  <!-- This outputs  an array of gids -->

      {% assign categoryEvents = category.events %}

      console.log('Category {{ category.system.id }} Events Array: {{ categoryEvents.values }}'); <!-- This outputs nothing. It just says "Category 105510141975 Events Array:" and then no array -->
-->
      {% assign eventNames = categoryEvents | map: 'name' %}

      console.log('Event Names in Category {{ category.system.id }}: {{ eventNames }}');
<!-- no eventNames, just "Event Names in Category 105510141975:" -->
     
    {% for event in categoryEvents %}
        <!-- Never runs inside this loop -->
        console.log('Event in Category {{ category.system.id }}: {{ event }}');
      {% endfor %}


Please help, I have tried many different things and reviewed every related post on this community to try to find an answer.

Accepted Solution (1)

Laza_Binaery
Shopify Partner
187 42 62

This is an accepted solution.

Hi @theevilgenius 

 

Try to change 

 {% assign categoryEvents = category.events %}

to

 {% assign categoryEvents = category.events.value %}

 

Then the rest should be correct.

Kind regards
Laza
www.binaery.com

View solution in original post

Replies 2 (2)

Laza_Binaery
Shopify Partner
187 42 62

This is an accepted solution.

Hi @theevilgenius 

 

Try to change 

 {% assign categoryEvents = category.events %}

to

 {% assign categoryEvents = category.events.value %}

 

Then the rest should be correct.

Kind regards
Laza
www.binaery.com
theevilgenius
Shopify Partner
4 0 1

Thank you! I had tried the plural (.values) but not the singular (.value).