0

I am using SvelteKit to pull a Prismic object. If I query a single document, I can render the data out fine. However, if I query everything and try to itemize it out in a loop, I am unable to extract the text value from the object.

Stringifying the object, I can see the data I want:

<pre>{ JSON.stringify(results, null, 2) }</pre> 

allows me to see the data

[
  {
    "type": "accounts",
    "data": {
      "created_at": "2021-08-25T05:00:00+0000",
      "name": [
        {
          "type": "heading2",
          "text": "John",
          "spans": []
        }
      ],
      "last_name": [
        {
          "type": "heading2",
          "text": "Doe",
          "spans": []
        }
      ],
    }
  },
  {
    "type": "accounts",
    "data": {
      "created_at": "2021-08-25T05:00:00+0000",
      "name": [
        {
          "type": "heading2",
          "text": "Jane",
          "spans": []
        }
      ],
      "last_name": [
        {
          "type": "heading2",
          "text": "Rey",
          "spans": []
        }
      ],
    }
  },
]

I am trying to get the last_name from the loop, ie Doe and Rey

{#each results as account}   
  <li>{ account.data.last_name }</li>    
{/each}

sends back an object. and

{#each results as account}
  <li>{ account.data.last_name.text }</li>
{/each}

throws an undefined error. I know it has something to with the name and last_name being in the brackets. I just don't know how to extract those in the loop.

I've tried:

account.data.last_name[0]
account.data.last_name["text]
account.data.last_name[0].text

and 20 more iterations of that. I feel I am missing something incredibly obvious here.

0

1 Answer 1

1

Just cross-posting my response from the Prismic forum :)

https://community.prismic.io/t/cannot-get-value-form-json-object-in-a-sveltekit-loop/6765/3?u=samlittlefair

Sign up to request clarification or add additional context in comments.

2 Comments

Added my response on that forum. If we get to an answer, I'll update SO so anyone with the same problem knows the solution.
Sam correctly answered this question over on the Prismic Community: community.prismic.io/t/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.