RN JS TypeError:undefined不是对象(评估' item.link.photo.photo_604')

时间:2018-01-29 19:03:43

标签: javascript reactjs react-native

我试图获取数据,数据输出到控制台,但错误发生得很顺利...

Json array vk neewsfeed!

代码:

export function ParseAttach(content){
 if (content == undefined) return true;
     let img = [];
     let index = 0;

    content.map(function(item,i){
     if (item.type == 'photo' || item.type == 'graffiti'){
          index++;
          img[0] = index;
        img[index] = item.photo.photo_604;
      } else if (item.type == 'link'){
          index++;
          img[0] = index;
          console.log(item.link.photo.photo_604);
         img[index] = item.link.photo.photo_604;
     }
   })
}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

您不需要额外的功能。

你可以这样做:

export function ParseAttach(content){
 if (content == undefined) return true;
     let img = [];
     let index = 0;

    content.map(function(item,i){
     if (item.type == 'photo' || item.type == 'graffiti'){
          index++;
          img[0] = index;
        img[index] = item.photo.photo_604;
      } else if (item.type == 'link'){
          index++;
          img[0] = index;  
          img[index] = item.link.photo && item.link.photo.photo_604;   
     }
   })
}