Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rana002 opened this issue Aug 23, 2023 · 12 comments

Comments

@rana002
Copy link

rana002 commented Aug 23, 2023

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}

@yeshwantthota
Copy link

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

@MihailoMilic
Copy link

@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.

@Niti9
Copy link

Niti9 commented Sep 3, 2023

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

@MihailoMilic
Copy link

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

@harikris001
Copy link

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

console shows the required attribute 'src' missing.

@kunalborkar2001
Copy link

Have anyone fixed this issue?

@kunalborkar2001
Copy link

@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

@cleph01
Copy link

cleph01 commented Dec 26, 2023

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.

@kunalborkar2001
Copy link

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

@Goniknr
Copy link

Goniknr commented Feb 27, 2024

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

@grunsho
Copy link

grunsho commented Mar 11, 2024

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.

@kunalborkar2001
Copy link

kunalborkar2001 commented Mar 11, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants