Youssouf Oumar
Youssouf Oumar

Reputation: 45825

TypeError: Cannot read properties of null (reading 'default') in Next.js

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>
  );
}

Upvotes: 14

Views: 22712

Answers (1)

Youssouf Oumar
Youssouf Oumar

Reputation: 45825

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 }

Upvotes: 42

Related Questions