NextJS app error when accessing post author information in Sanity.io
21 replies
Last updated: Feb 18, 2023
D
Hi, I am trying to figure out why
_createdAt: '2023-01-21T23
:38:11Z', _id: '9cfb89fd-66cb-45f9-b310-b2990782c325',
_rev: '0yPlmSh0SROjoBusCs2gsT',
_type: 'post',
_updatedAt: '2023-02-18T15
:43:28Z',
authors: [ [Object] ], banner_image: {
_type: 'image',
alt_text: 'Test Caption',
caption: 'Test Caption'
},
Where as this query ``*[_type == "post" && slug.current == $slug][0]`,{ slug },)` returns the expanded results for
Authors
_type: 'post',
_updatedAt: '2023-02-18T15
:43:28Z',
authors: [
{
_key: '97b1a4c0eb70',
_type: 'author',
*[_type == "post"] | order(publish_date desc)); returns [Object] in the results{
_createdAt: '2023-01-21T23
:38:11Z', _id: '9cfb89fd-66cb-45f9-b310-b2990782c325',
_rev: '0yPlmSh0SROjoBusCs2gsT',
_type: 'post',
_updatedAt: '2023-02-18T15
:43:28Z',
authors: [ [Object] ], banner_image: {
_type: 'image',
alt_text: 'Test Caption',
caption: 'Test Caption'
},
Where as this query ``*[_type == "post" && slug.current == $slug][0]`,{ slug },)` returns the expanded results for
Authors
_type: 'post',
_updatedAt: '2023-02-18T15
:43:28Z',
authors: [
{
_key: '97b1a4c0eb70',
_type: 'author',
Feb 18, 2023, 4:22 PM
P
data rendered in a console or in a terminal window truncates large objects to save memory. when you scope the query to single post the rendered object(array) is visible. in both instance they are “expanded” or if you dumped this query into a file they would both be fully visible.
Feb 18, 2023, 5:14 PM
P
this is just how it is visible in the preview but the data is fully accessible in both instances.
Feb 18, 2023, 5:17 PM
P
I hope thats clear. it has nothing to do with GROK, its only visual.
Feb 18, 2023, 5:21 PM
P
here is a article on how to get the full print of the return value if you are dumping this using console https://dev.to/samueldjones/using-console-dir-to-print-javascript-objects-in-the-console-3jkh
Feb 18, 2023, 5:27 PM
D
Thanks for your replies ... however, in my NextJS app, I get the following error ...
TypeError: Cannot read properties of undefined (reading '0')
On this statement
post.authors[0].profile_picture
So not sure what I am doing wrong.
Thanks in advance for your insight
TypeError: Cannot read properties of undefined (reading '0')
On this statement
post.authors[0].profile_picture
So not sure what I am doing wrong.
Thanks in advance for your insight
Feb 18, 2023, 10:23 PM
P
not sure. would need to see your code. I assume posts are being iterated over?
Feb 18, 2023, 10:31 PM
P
what happens when you log
post?
Feb 18, 2023, 10:32 PM
P
is there a possibility that some posts dont have authors? cause then you need to conditionally check to make sure authors exists first.
Feb 18, 2023, 10:34 PM
D
There is only 1 post in the result set ... A post can have multiple authors and an getting the 1st author's info ... [0]...
<div className="w-1/2"> <Image src={post.authors[0].profile_picture} height={100} width={100} alt={post.authors[0].first_name} /> </div>
Feb 18, 2023, 10:34 PM
D
The post object is passed in...
const Post = ({ post }) => { ....
const Post = ({ post }) => { ....
Feb 18, 2023, 10:35 PM
P
console log
post
Feb 18, 2023, 10:36 PM
P
verify that the post object has the properties you need.
Feb 18, 2023, 10:36 PM
D
Here is the log ...
[
{
_createdAt: '2023-01-21T23
:38:11Z', _id: '9cfb89fd-66cb-45f9-b310-b2990782c325',
_rev: '0yPlmSh0SROjoBusCs2gsT',
_type: 'post',
_updatedAt: '2023-02-18T15
:43:28Z', authors: [ [Object] ],
banner_image: {
_type: 'image',
alt_text: 'Test Caption',
caption: 'Test Caption'
},
body: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object]
],
category: [ 'Strategy' ],
headline: 'Lets make a big deal of this post. We want to bring in traffic to this post, so lets make it happen',
publish_date: '2023-01-21T23
:34:00.000Z', slug: { _type: 'slug', current: 'test-post' },
title: 'This is the title of the post2'
}
]
error - pages/posts/[slug].js (202:36) @ Post
error - TypeError: Cannot read properties of undefined (reading '0')
at Post (webpack-internal
:///./pages/posts/[slug].js:372:70) at processChild (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/react-dom/cjs/react-dom-server.node.development.js
:3353:14) at resolve (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/react-dom/cjs/react-dom-server.node.development.js
:3270:5) at ReactDOMServerRenderer.render (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/react-dom/cjs/react-dom-server.node.development.js
:3753:22) at ReactDOMServerRenderer.read (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/react-dom/cjs/react-dom-server.node.development.js
:3690:29) at Object.renderToString (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/react-dom/cjs/react-dom-server.node.development.js
:4298:27) at Object.renderPage (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/next/dist/server/render.js
:662:46) at Object.defaultGetInitialProps (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/next/dist/server/render.js
:332:67) at MyDocument.getInitialProps (webpack-internal
:///./node_modules/next/dist/pages/_document.js:21:20) at Object.<anonymous> (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/next/dist/shared/lib/utils.js
:75:33) { page: '/posts/[slug]'
}
200 | <div className="w-1/2">
201 | <Image
[
{
_createdAt: '2023-01-21T23
:38:11Z', _id: '9cfb89fd-66cb-45f9-b310-b2990782c325',
_rev: '0yPlmSh0SROjoBusCs2gsT',
_type: 'post',
_updatedAt: '2023-02-18T15
:43:28Z', authors: [ [Object] ],
banner_image: {
_type: 'image',
alt_text: 'Test Caption',
caption: 'Test Caption'
},
body: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object]
],
category: [ 'Strategy' ],
headline: 'Lets make a big deal of this post. We want to bring in traffic to this post, so lets make it happen',
publish_date: '2023-01-21T23
:34:00.000Z', slug: { _type: 'slug', current: 'test-post' },
title: 'This is the title of the post2'
}
]
error - pages/posts/[slug].js (202:36) @ Post
error - TypeError: Cannot read properties of undefined (reading '0')
at Post (webpack-internal
:///./pages/posts/[slug].js:372:70) at processChild (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/react-dom/cjs/react-dom-server.node.development.js
:3353:14) at resolve (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/react-dom/cjs/react-dom-server.node.development.js
:3270:5) at ReactDOMServerRenderer.render (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/react-dom/cjs/react-dom-server.node.development.js
:3753:22) at ReactDOMServerRenderer.read (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/react-dom/cjs/react-dom-server.node.development.js
:3690:29) at Object.renderToString (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/react-dom/cjs/react-dom-server.node.development.js
:4298:27) at Object.renderPage (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/next/dist/server/render.js
:662:46) at Object.defaultGetInitialProps (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/next/dist/server/render.js
:332:67) at MyDocument.getInitialProps (webpack-internal
:///./node_modules/next/dist/pages/_document.js:21:20) at Object.<anonymous> (/Users/donaldhook/Library/Mobile Documents/com~apple~CloudDocs/NextJS/fullonconsulting/node_modules/next/dist/shared/lib/utils.js
:75:33) { page: '/posts/[slug]'
}
200 | <div className="w-1/2">
201 | <Image
202 | src={post.authors[0].profile_picture}
Feb 18, 2023, 10:37 PM
P
ok well now its obv you need
post[0].authors[0].profile_picture
Feb 18, 2023, 10:38 PM
D
I tried that ... I thought the post was an array... but it was not ...
Feb 18, 2023, 10:40 PM
D
Same error ...
Feb 18, 2023, 10:40 PM
P
? you’re not doing it though
Feb 18, 2023, 10:41 PM
P
post is an array.
Feb 18, 2023, 10:43 PM
D
That was it ...
Feb 18, 2023, 10:44 PM
D
So the one query since I am using the slug as a parameter, returns a single object and the query which gets all the posts, is an array ...
Feb 18, 2023, 10:45 PM
D
Thank you for your time and help!!
Feb 18, 2023, 10:45 PM
Sanity.io – build remarkable experiences at scale
Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.