Studentofcode
Studentofcode

Reputation: 45

Gatsby: TypeError: Cannot read property 'map' of null

I get this error in my Gatsby app that pulls places data for Author. the error shows when map() points of author.social is null

TypeError: Cannot read property 'map' of null

I already tried to give a default value, but its seems not working

This is the default code :

authors: ({ node: author }) => {
    return {
      ...author,
      social: author.social.map(s => ({ url: s })),
    };
  },

This is the my change :

authors: ({ node: author }) => {
      return {
        ...author,
        social: author.social.map(s => {
          if (s === null) {
            return ({ url: 'https://www.mywebsite.com' });
          } else {
            return ({ url: s });
          }
        })
        ,
      };
    },

Any help would be very helpful,

Thanks

Upvotes: 0

Views: 720

Answers (1)

Oriol Grau
Oriol Grau

Reputation: 629

This is why I would do:

authors: ({ node: author }) => {
    // This to inspect what there is inside the variable 
    console.log(author);
    // And something like this to avoid the error:
    if(author.social && author.social.length > 0){
      // code here
    }
  });

Upvotes: 1

Related Questions