2016-11-08 15 views
-2

私はAPIを呼び出して、この情報をレンダリングするために、たSuperAgentを使用していますが、私は間違った方法でそれをやっているデータスーパーエージェントを使用してAPIを呼び出しますか?

{ 
 
    "data": { 
 
    "id": , 
 
    "title": , 
 
    "description": , 
 
    "old_price": "100", 
 
    "new_price": "50", 
 
    "size": "50", 
 
    "quantity": 20, 
 
    }, 
 
    "errors": null, 
 
    "code": 1 
 
}

のこのブランチで構成APIを持っています。

ここに私のコード

import React from 'react'; 
 
import {render} from 'react-dom'; 
 
import superagent from 'superagent'; 
 
import Layout from './components/Layout'; 
 

 
class App extends React.Component{ 
 
    constructor(){ 
 
    super() 
 
    this.state={ 
 
     name:'' 
 
    } 
 
    } 
 
    componentDidMount(){ 
 
     console.log('componentDidMount'); 
 

 
     const url='http://...'; 
 
     superagent 
 
     .get(url) 
 
     .query(null) 
 
     .set('Accept', 'application/json') 
 
     .end ((error, response)=>{ 
 
      const title=response.body.response 
 
      console.log(JSON.stringify(title)); 
 
      
 
      this.setState({ 
 
       name:title 
 
      }) 
 
     }) 
 
     
 
    } 
 
    render(){ 
 
    return(
 
     <Layout data={this.state}/> 
 
    ) 
 
    } 
 
} 
 

 

 
render(<App />, document.getElementById('app'));

ここで私はそれ

​​

しかし、レンダリングされていないレンダリング、私は私が間違っワットでやっていると思いますay。あなたはそれで私を助けてくれますか?私にとって

答えて

2
const title=response.body.response 

が最も可能性の高い

+0

ねえ、それが働いたのuに感謝

const title=response.body 

であるべきもちろん、いくつか調整を加えましたが、これが出発点でした) – Feruza

0

このような何かが働いている:!

import request from 'superagent'; 

getFooFromServer() { 
    const url='http://...'; 
    request.get(`${url}/foo`) 
    .accept('json') 
    .then(res => { 
     //Do something with your data 
    } 
    catch(error => { 
     //Do something with the error 
    }; 
} 
関連する問題