Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 盖茨比:类型错误:无法读取null的属性“map”_Javascript_Gatsby_Contentful - Fatal编程技术网

Javascript 盖茨比:类型错误:无法读取null的属性“map”

Javascript 盖茨比:类型错误:无法读取null的属性“map”,javascript,gatsby,contentful,Javascript,Gatsby,Contentful,我在我的盖茨比应用程序中得到了这个错误,它为作者提取了位置数据。当author.social的映射点为空时,将显示错误 TypeError:无法读取null的属性“map” 我已经尝试给出一个默认值,但它似乎不起作用 这是默认代码: authors: ({ node: author }) => { return { ...author, social: author.social.map(s => ({ url: s })), }; },

我在我的盖茨比应用程序中得到了这个错误,它为作者提取了位置数据。当author.social的映射点为空时,将显示错误

TypeError:无法读取null的属性“map”

我已经尝试给出一个默认值,但它似乎不起作用

这是默认代码:

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

您可以在返回之前console.logauthor检查此author变量中的实际内容。我认为你应该先检查变量是null还是emply,然后再将map应用到它。。。好的,是的,我正在更新答案,谢谢:-如果@Studentofcode中的内容取决于您得到的响应