# Cannot populate partial with Sanity API

**URL:** <https://discourse.gohugo.io/t/cannot-populate-partial-with-sanity-api/31701>\
**Category:** support\
**Created:** [March 11, 2021, 5:14pm UTC](https://discourse.gohugo.io/t/cannot-populate-partial-with-sanity-api/31701 "2021-03-11T17:14:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarcMartin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/marcmartin/32/15735_2.png) [@MarcMartin](https://discourse.gohugo.io/u/MarcMartin)\
**Post date:** [March 11, 2021, 5:14pm UTC](https://discourse.gohugo.io/t/cannot-populate-partial-with-sanity-api/31701/1 "2021-03-11T17:14:45Z")

</div>

Good morning everyone. I’m trying to build a site on Hugo, and the client wants to be able to add projects to the site as they complete them. To do this, I was trying to set up Sanity CMS. Basically, i need a partial that pulls data from the sanity api to load up the projects they want displayed. I’ll attach some of the code that i have. You can read the api data if you type the endpoint url that i have in the code.

Heres the code from the layout file

```auto
	{{ block "projects" . }}
		   {{ $dataJ := getJSON "https://76la3n1i.api.sanity.io/v1/data/query/production?query=*[]{name,description,image}" }}
		   {{ range $dataJ}}
			   {{partial "project.html" }}
			 {{ end }}
		{{ end }}

```

And heres the partial that i’m trying to load:

```auto
 <div>
  <img src="{{.image.asset._ref}}" alt=""">
  <h3">{{ .result.name . }}</h3>
  <p">{{ .result.description}}</p>
</div>

```

Dont mind the extra quotes on the html elements. Had to add them for this post to display correctly in the forum

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [March 11, 2021, 5:23pm UTC](https://discourse.gohugo.io/t/cannot-populate-partial-with-sanity-api/31701/2 "2021-03-11T17:23:55Z")

</div>

Please note, that code blocks need to be wrapped either in backticks manually: ``` or within the preformatted text block provided by the \</\> button in the post UI, otherwise the forum software does not render raw code.

---

<div class="post-metadata">

**Author:** ![MarcMartin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/marcmartin/32/15735_2.png) [@MarcMartin](https://discourse.gohugo.io/u/MarcMartin)\
**Post date:** [March 11, 2021, 5:26pm UTC](https://discourse.gohugo.io/t/cannot-populate-partial-with-sanity-api/31701/3 "2021-03-11T17:26:04Z")

</div>

ok thanks. sorry i didnt realize that

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [March 11, 2021, 5:28pm UTC](https://discourse.gohugo.io/t/cannot-populate-partial-with-sanity-api/31701/4 "2021-03-11T17:28:39Z")

</div>

Regarding your question it appears that your issue is due to the context of `$dataJ` being lost when trying to access it from a partial.

Either try removing the partial or call the keys of the data file by prepending the `$` dollar sign e.g. `$.result.name`

---

<div class="post-metadata">

**Author:** ![MarcMartin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/marcmartin/32/15735_2.png) [@MarcMartin](https://discourse.gohugo.io/u/MarcMartin)\
**Post date:** [March 11, 2021, 5:42pm UTC](https://discourse.gohugo.io/t/cannot-populate-partial-with-sanity-api/31701/5 "2021-03-11T17:42:43Z")

</div>

ok so i took out the partial, and just included the div inside the range. This is what i get

Error: Error building site: failed to render pages: render of “taxonomy” failed: “C:\Users\marcl\OneDrive\Web Development\Krahn Construction\layouts\_default\baseof.html:30:36”: execute of template  
failed: template: \_default/list.html:30:36: executing “projects” at \<.name\>: can’t evaluate field name in type interface {}

---

<div class="post-metadata">

**Author:** ![alexandros](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@alexandros](https://discourse.gohugo.io/u/alexandros)\
**Post date:** [March 11, 2021, 5:53pm UTC](https://discourse.gohugo.io/t/cannot-populate-partial-with-sanity-api/31701/6 "2021-03-11T17:53:11Z")

</div>

> [@MarcMartin](#):
>
> OneDrive

First of all it is a bad idea to keep a Hugo project in Onedrive, Dropbox and other cloud storage providers (there have been posts in this forum from users that ended up with corrupted Git repositories etc)

Second from the error it seems that you do not have that key everywhere in your data set.  
Try `{{ with .name }}{{ . }}{{ end }}`

It is always best to conditionally call keys or parameters in Hugo templates to avoid this type of errors.

---

<div class="post-metadata">

**Author:** ![MarcMartin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/marcmartin/32/15735_2.png) [@MarcMartin](https://discourse.gohugo.io/u/MarcMartin)\
**Post date:** [March 11, 2021, 7:31pm UTC](https://discourse.gohugo.io/t/cannot-populate-partial-with-sanity-api/31701/7 "2021-03-11T19:31:03Z")

</div>

ok i didnt realize that about onedrive. only reason i had them there was if my computer quit or something, id always have a backup. Is there another solution for that you’d recommend, or only use git? and thanks for the help. that worked correctly!

---

<div class="post-metadata">

**Author:** ![pamubay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pamubay/32/17838_2.png) [@pamubay](https://discourse.gohugo.io/u/pamubay)\
**Post date:** [March 11, 2021, 7:34pm UTC](https://discourse.gohugo.io/t/cannot-populate-partial-with-sanity-api/31701/8 "2021-03-11T19:34:21Z")

</div>

> [@MarcMartin](#):
>
> ```diff
> {{ block "projects" . }}
> {{ $dataJ := getJSON "https://76la3n1i.api.sanity.io/v1/data/query/production?query=*[]{name,description,image}" }}
> {{ range $dataJ}}
> -- {{partial "project.html" }}
> ++ {{partial "project.html" . }}
> {{ end }}
> {{ end }}
> 
> ```

you forgot to pass the `.` dot context.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [March 13, 2021, 7:34pm UTC](https://discourse.gohugo.io/t/cannot-populate-partial-with-sanity-api/31701/9 "2021-03-13T19:34:45Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
