2017-03-31 83 views
1

我有点麻烦,我有JSON数据正在提取,然后数据呈现。然而,在渲染中,我需要执行另一个获取,因为我试图渲染的数据依赖于第一个JSON。检索render()中的返回值?

你可以看到我试图抓住返回的值,但它是未定义的。

所以取这个样子的:

getFeaturedImage(thumbnail_json) { 
    fetch(thumbnail_json) 
     .then((response) => response.json()) 
     .then((responseData) => { 
     return responseData.media_details.sizes.medium.source_url; 
     }) 
     .done(); 
    } 

和renderPost()是:

renderPosts() { 
    contents = this.state.posts.map((post) => { 
     let postDate = Moment(post.date).format('LLL'); 

     if (post._links['wp:featuredmedia']) { 
     console.log('has featured image'); 
     let thumbnail_json = post._links['wp:featuredmedia'][0].href; 
     this.getFeaturedImage(thumbnail_json); 
     console.log(this.getFeaturedImage(thumbnail_json)); // <----- UNDEFINED? 
     } else { 
     console.log('no featured image'); 
     } 

     return (
     <View key={post.id} style={ postStyles.postContainer }> 
      <Text style={postStyles.postTitle}> 
      {post.title.rendered} 
      </Text> 
      <Text style={postStyles.postDate}> 
      {postDate} 
      </Text> 
      <View style={ postStyles.excerptContainer }> 
      <HTMLView stylesheet={htmlStyles} 
       value={post.excerpt.rendered} 
      /> 
      </View> 
     </View> 
    ); 
    }); 
    return (
     <ScrollView> 
     <View style={baseStyles.container}> 
      {contents} 
     </View> 
     </ScrollView> 
    ); 
    } 

回答

1

您的功能目前不返回任何东西,前面加一个return中的抓取:

getFeaturedImage(thumbnail_json) { 
    return fetch(thumbnail_json) 
    .then((response) => response.json()) 
    .then((responseData) => { 
    return responseData.media_details.sizes.medium.source_url; 
    }) 
    .done(); 

}

0

可以在getFeaturedImage传递一个回调函数作为参数,调用上success

mycallback(result) { 
    //do what you want with result 
} 
getFeaturedImage(thumbnail_json, callback) { 
    fetch(thumbnail_json) 
     .then((response) => response.json()) 
     .then((responseData) => { 
     callback(responseData.media_details.sizes.medium.source_url); 
     }) 
     .done(); 
    } 

renderPosts() { 
    contents = this.state.posts.map((post) => { 
     let postDate = Moment(post.date).format('LLL'); 

     if (post._links['wp:featuredmedia']) { 
     console.log('has featured image'); 
     let thumbnail_json = post._links['wp:featuredmedia'][0].href; 
     this.getFeaturedImage(thumbnail_json, this.mycallback); 
     console.log(this.getFeaturedImage(thumbnail_json)); // <----- UNDEFINED? 
     } else { 
     console.log('no featured image'); 
     } 

     return (
     <View key={post.id} style={ postStyles.postContainer }> 
      <Text style={postStyles.postTitle}> 
      {post.title.rendered} 
      </Text> 
      <Text style={postStyles.postDate}> 
      {postDate} 
      </Text> 
      <View style={ postStyles.excerptContainer }> 
      <HTMLView stylesheet={htmlStyles} 
       value={post.excerpt.rendered} 
      /> 
      </View> 
     </View> 
    ); 
    }); 
    return (
     <ScrollView> 
     <View style={baseStyles.container}> 
      {contents} 
     </View> 
     </ScrollView> 
    ); 
    } 
+0

它的出现,我已经找到另一种解决方案,实际使用API​​ IM还有一个JSON其输出的所有数据,这意味着我可以提出单一请求。 – narutoramen