Hi, I'm having difficulty displaying some posts. I've followed nearly every tutorial out there and the official Prismic documentation, but I cannot get my head around what I am doing wrong. I keep getting the error
TypeError: articles.map is not a function
I know that I have set everything up to pull data in fine, as the elements from my getSingle are working. Here is my code for this specific data:
export async function getStaticProps() {
// The single is working fine
const document = await Client.getSingle('home')
const articles = await Client.getByType('articles')
// The articles data is logging fine in the console
console.log(articles)
return {
props: {
articles,
document
}
}
}
gives the error: TypeError: articles.map is not a function. This is how I have seen this in countless youTube videos - where am I going wrong? Any help is much appreciated.
So doing an articles.results.map gives this error now:
error - Error: Objects are not valid as a React child (found: object with keys {type, text, spans}). If you meant to render a collection of children, use an array instead.
error - Error: Objects are not valid as a React child (found: object with keys {type, text, spans}). If you meant to render a collection of children, use an array instead.
This error means that you're trying to print an object straight into a React object, which doesn't work. I'm assuming there's an {articles.results.map} or {articles} lost somewhere inside your component's return function.
Can you try doing adding console.log(articles) into your page's code and share it the console output? Also, the more code you share the easier it will be for me to help you so if you have a public repo for that project that would be great.
Thanks Vitor, my code is up here - https://github.com/JonQuayle/Next-js-starter - feel free to have a poke around. This specific error is occurring in the index.js file of the pages folder.
I'm a little lost as to whether the data is getting lost inside the function, I'm not sure how I can tell.
I saw that you've made some changes to the repo since your last message. Did you get to understand what is happening?
The errors you've reported are more of JS/React errors than anything to do with Prismic, so I'm not sure how can I further help you here.
One thing that can help is going through Next.js docs, I find that this page in particular can help a lot. Once you get to know Next.js better everything Prismic will seem much easier
Not really, I was trying some things whilst trying to figure out what was happening. Thanks for your help though. I'm just going to keep working at it and see if it works. I am console logging the articles, so they are being queried correctly but whatever method I try to convert those article objects into an array it throws a wobbly every time. Seems kinda long winded to grab a few articles but then again I'm not great at a lot of JS.
Welcome to the Prismic community and thanks for reaching out to us.
When you perform a query by getByType, you will get a results array containing all matching results. You are storing in an articles array. For example, if you want to retrieve a Key text field, It should be like this: