15

Earlier my Next.js application started throwing TypeError: Cannot read properties of null (reading 'default') without me changing anything in my code where I'm rendering some articles from our CMS.

Here is the complete log:

enter image description here

And here is my code:

import Image from "next/image";

export default function Articles({ articles }) {
  return (
    <div className="articles">
      {articles.map((article) => (
        <div className="article" key={article.id}>
          <div className="text">
            <h2>{article.title}</h2>
            <p>{article.content}</p>
          </div>
          <div className="image">
            <Image src={article.image} alt="" height={900} width={900} layout="responsive" />
          </div>
        </div>
      ))}
    </div>
  );
}
0

1 Answer 1

44

After some research, I found this GitHub discussion, then realized that a colleague has deleted the image of one of our articles from our CMS. If you ever come across this, the solution is to render the Image tag after a check, like so:

{ article.image ? <Image src={article.image} alt="" height={900} width={900} layout="responsive" /> : null }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.