-
ProblemI’m trying to loop through a collection of pages (generated by Eleventy’s pagination feature) and access each of its computed front-matter. Unfortunately, the array just renders a string reading Expected data: {
"images": {
"nodes": [
{
"altText": "Lorem ipsum",
"src": "https://cdn.shopify.com/s/files/…"
},
{
"altText": "Lorem ipsum",
"src": "https://cdn.shopify.com/s/files/…"
},
{
"altText": "Lorem ipsum",
"src": "https://cdn.shopify.com/s/files/…"
}
]
}
}ContextI’m using Shopify’s Storefront API to fetch, an array of the products in store and their metadata.
// shopify.js
const fetch = require("node-fetch");
require("dotenv").config();
module.exports = async () => {
try {
const result = await fetch(process.env.SHOPIFY_API_ENDPOINT, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Shopify-Storefront-Access-Token": process.env.SHOPIFY_STOREFRONT_API_TOKEN,
},
body: JSON.stringify({
query: `{
shop {
// …
}
products(first: 100) {
edges {
node {
// …
images(first: 100) {
nodes {
altText
src
}
}
// …
}
}
}
collections(first: 100) {
// …
}
localization {
// …
}
}`
}),
}).then((res) => res.json())
if (result.errors) {
console.log({
errors: result.errors
})
} else if (!result || !result.data) {
console.log({
result
})
return "No results found."
}
return {
shop: result.data.shop,
products: result.data.products.edges,
collections: result.data.collections.edges,
lang: result.data.localization.language
}
} catch (error) {
console.log(error)
}
};# products.liquid
tags: "products"
pagination:
data: "shopify.products"
alias: "product"
size: 1
addAllPagesToCollections: true
eleventyComputed:
title: "{{ product.node.title }}"
price: "{{ product.node.priceRange.minVariantPrice.amount }}"
images: "{{ product.node.images.nodes }}"
permalink: "/{{ page.fileSlug }}/{{ product.node.handle }}/"<!-- index.liquid -->
{% for product in collections.products %}
{% for image in product.data.images %}
<img src="{{ image.src }}" alt="{{ image.altText }}">
{% endfor %}
{% endfor %}What I know
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
I think the issue is that this will stringify the array into a string of "[object Object]": eleventyComputed:
images: "{{ product.node.images.nodes }}"I'd have to try rebuilding everything from scratch to see if it'd have the same behavior if you used |
Beta Was this translation helpful? Give feedback.
I think the issue is that this will stringify the array into a string of "[object Object]":
I'd have to try rebuilding everything from scratch to see if it'd have the same behavior if you used
---jsfront matter instead, or moved the front matter into an external products.11tydata.js file and tried aliasing the computedimagesas an array instead of a string.https://www.11ty.dev/docs/data-computed/