簡體   English   中英

蓋茨比:類型錯誤:無法讀取 null 的屬性“地圖”

[英]Gatsby: TypeError: Cannot read property 'map' of null

我在為作者提取地點數據的 Gatsby 應用程序中收到此錯誤。 當 author.social 的 map() 點為 null 時,錯誤顯示

TypeError: Cannot read property 'map' of null

我已經嘗試提供默認值,但它似乎不起作用

這是默認代碼:

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

這是我的改變:

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

任何幫助都會非常有幫助,

謝謝

這就是為什么我會這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM