project_next_14_ai_prompt_sharing icon indicating copy to clipboard operation
project_next_14_ai_prompt_sharing copied to clipboard

Cannot read properties of undefined (reading 'image' in components/promptcard.jsx

Open rana002 opened this issue 1 year ago • 12 comments

error TypeError: Cannot read properties of undefined (reading 'image') at PromptCard (./components/PromptCard.jsx:46:51) 35 | > 36 | <Image

37 | src={post.creator.image} | ^ 38 | alt='user_image' 39 | width={40} 40 | height={40}

rana002 avatar Aug 23 '23 13:08 rana002

@rana002 Getting the same error, have you found any solution?

yeshwantthota avatar Aug 24 '23 13:08 yeshwantthota

@rana002 @yeshwantthota problem is that the server renders the image before it is able to fetch post.creator.image resulting in undefined. You can fix that by simply adding ? between creator and image like this post.creator?.image or you can use a conditional {post.creator && post.creator.image} so that it only renders when it acquires the needed data.

MihailoMilic avatar Sep 02 '23 09:09 MihailoMilic

@MihailoMilic bro can you send me full source code of this Promptopia website

Niti9 avatar Sep 03 '23 02:09 Niti9

@Niti9 just change post.creator.image to post.creator?.image

MihailoMilic avatar Sep 04 '23 07:09 MihailoMilic

@MihailoMilic but the content is missing. it is rendered like this Screenshot 2023-12-14 150650

console shows the required attribute 'src' missing.

harikris001 avatar Dec 14 '23 09:12 harikris001

Have anyone fixed this issue?

kunalborkar2001 avatar Dec 25 '23 05:12 kunalborkar2001

@MihailoMilic but the content is missing. it is rendered like this Screenshot 2023-12-14 150650

console shows the required attribute 'src' missing.

Same issue

kunalborkar2001 avatar Dec 25 '23 05:12 kunalborkar2001

Same issue. Optional chaining not fixing the issue. I think it has to do with the "populate" method in the GET posts end-point [const prompts = await Prompt.find({}).populate("creator")]. It doesn't seem do be doing anything.

[EDIT] The problem was how I was accessing the userId from the session in my createPrompt function. In my post request, I was sending {..., userId: session?.user._id} <-- the session knows the userId as user.id --> so the correct reference should be: {..., userId: session?.user.id}

Everything working fine for me now. Hope this helps.

cleph01 avatar Dec 26 '23 19:12 cleph01

this error comes when you use cluster url for the mongo and not on the local mongo url

kunalborkar2001 avatar Feb 23 '24 07:02 kunalborkar2001

I deleted everything in my database and now it's fine.

Goniknr avatar Feb 27 '24 19:02 Goniknr

Fixed the issue with the following steps:

  1. Change the content of "images" on the next.config.mjs file with the following code:
images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'lh3.googleusercontent.com',
        port: '',
        pathname: '/**',
      },
    ],

This is because In Next.js Version 14, domains configuration has been deprecated in favour of remotePatterns.

  1. Use the optional chaining in create-prompt/page.jsx: post.creator?.image
  2. Clear the database and it will work perfectly.

grunsho avatar Mar 11 '24 05:03 grunsho

You are God bro ❤️

On 11-Mar-2024, at 11:22, Alvaro Manterola @.***> wrote:

Fixed the issue with the following steps:

Change the content of "images" on the next.config.mjs file with the following code: images: { remotePatterns: [ { protocol: 'https', hostname: 'lh3.googleusercontent.com', port: '', pathname: '/**', }, ], This is because In Next.js Version 14, domains configuration has been deprecated in favour of remotePatterns.

Use the optional chaining in create-prompt/page.jsx: post.creator?.image Clear the database and it will work perfectly. — Reply to this email directly, view it on GitHub https://github.com/adrianhajdin/project_next_14_ai_prompt_sharing/issues/62#issuecomment-1987679747, or unsubscribe https://github.com/notifications/unsubscribe-auth/BA34AXJZA553E4NG2KTMDV3YXVBDFAVCNFSM6AAAAAA33QJ5MOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOBXGY3TSNZUG4. You are receiving this because you commented.

kunalborkar2001 avatar Mar 11 '24 08:03 kunalborkar2001

@rana002 @yeshwantthota problem is that the server renders the image before it is able to fetch post.creator.image resulting in undefined. You can fix that by simply adding ? between creator and image like this post.creator?.image or you can use a conditional {post.creator && post.creator.image} so that it only renders when it acquires the needed data.

it solve image issue but TypeError: Cannot read properties of undefined (reading '_id') i got these new error

udayapex1 avatar Oct 07 '24 15:10 udayapex1

if (post.creator._id === session?.user.id) return router.push("/profile");

in these line i got an error TypeError: Cannot read properties of undefined (reading '_id')

udayapex1 avatar Oct 07 '24 16:10 udayapex1

@MihailoMilic but the content is missing. it is rendered like this Screenshot 2023-12-14 150650 console shows the required attribute 'src' missing.

Same issue

can you provide screenshot which you shared

udayapex1 avatar Oct 07 '24 16:10 udayapex1