Javascript 盖茨比:类型错误:无法读取null的属性“map”
我在我的盖茨比应用程序中得到了这个错误,它为作者提取了位置数据。当author.social的映射点为空时,将显示错误 TypeError:无法读取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 })), }; },
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中的内容取决于您得到的响应