简体   繁体   English

在反应中不获取数据

[英]fetch not returning data in react

I'm new to react, i'm having difficulty getting data for a single book out of list, be passed through via axios' get method. 我是新来的反应者,我很难通过axios的get方法传递单本书的数据。 I think it has something to do with the url, but I have been unable to get fix it. 我认为这与网址有关,但我无法修复它。

Here's my code: 这是我的代码:

 export function loadBook(book){ return dispatch => { return axios.get('http://localhost:3000/api/books/book/:id').then(book => { dispatch(loadBookSuccess(book.data)); console.log('through!'); }).catch(error => { console.log('error'); }); }; } //also tried this export function loadBook(id){ return dispatch => { return axios.get('http://localhost:3000/api/books/book/' + {id}).then(book => { dispatch(loadBookSuccess(book.data)); console.log('through!'); }).catch(error => { console.log('error'); }); }; } 

Html code that contains a variable link to each individual book 包含指向每本书的变量链接的HTML代码

  <div className="container"> <h3><Link to={'/book/' + book._id}> {book.title}</Link></h3> <h5>Author: {book.author.first_name + ' ' + book.author.family_name}</h5> <h4>Summary: {book.summary}</h4> <BookGenre genre={genre} /> </div> 

link in Route: 路线中的链接:

 <Route path="/book/:id" component={BookPage} /> 

Edit: code for the book component 编辑:书籍组件的代码

 class BookPage extends React.Component{ render(){ const book = this.props; const genre = book.genre; console.log(book); return( <div> <div> <h3> {book.title}</h3> <h5>Author: {book.author.first_name + ' ' + book.author.family_name}</h5> <h4>Summary: {book.summary}</h4> <BookGenre genre={genre} /> </div> </div> ); } } BookPage.propTypes = { book: PropTypes.object.isRequired }; //setting the book with mapStateToProps function mapStateToProps (state, ownProps){ let book = {title: '', author: '', summary: '', isbn: '', genre: []}; const bookid = ownProps.params._id; if(state.books.length > 0){ book = Object.assign({}, state.books.find(book => book.id)); } return { book: book }; } function mapDispatchToProps (dispatch) { return { actions: bindActionCreators(loadBook, dispatch) }; } export default connect(mapStateToProps, mapDispatchToProps)(BookPage); 

Instead of doing this:- 而不是这样做:

axios.get('http://localhost:3000/api/books/book/' + {id})

You should do like this:- 您应该这样做:

axios.get(`http://localhost:3000/api/books/book/${id}`)

So your action.js might look like this:- 因此,您的action.js可能如下所示:-

export function loadBook(id){
    const request = axios.get(`http://localhost:3000/api/books/book/${id}`);
    return dispatch  => {
        request.then(book => {
            dispatch(loadBookSuccess(book.data));
        }).catch(error => {
            console.log('error');
        })
    };
}

Since the id, you have passed it seems to be a string so it can be concatenated using ES6 template strings and make sure you wrap your strings in backtick . 自从id以来,您已经传递了它似乎是一个字符串,因此可以使用ES6模板字符串将其串联起来,并确保将字符串包装在backtick中。 or you can do it by + operator, also make sure you pass id as a parameter in your loadbook function so that you can join it to your URL. 或者您可以通过+运算符进行操作,也请确保在loadbook函数loadbook id作为参数传递,以便可以将其加入URL。

Figured out the solution to this problem. 找出解决此问题的方法。 My mistake was that I failed to send the id of the item I along with the api call. 我的错误是我无法将我的商品的ID与api调用一起发送。 Using componentDidMount and sending the dynamic id from the url params solved this problem for me. 使用componentDidMount并从url参数发送动态ID为我解决了这个问题。

Thank you, @Vinit Raj, I guess I was too much of a rookie then. 谢谢@Vinit Raj,我想那时候我真是个菜鸟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM