简体   繁体   English

尝试向 React 显示数据

[英]Trying to display data to React

New to React, trying to display data (inside of a bootstrap modal) from MongoDB to React with an axios ajax request. React 的新手,尝试显示来自 MongoDB 的数据(在引导模式内)以使用 axios ajax 请求进行反应。 Postman shows to be correct. Postman 显示是正确的。 React throws "TypeError: this.state.reviews.map is not a function" , which tells me it is not an array since .map() is an array method, but Im saving the state as an array. React 抛出"TypeError: this.state.reviews.map is not a function" ,这告诉我它不是一个数组,因为.map()是一个数组方法,但我将 Z9ED39E2EA931586B6EZA98 保存为一个数组。 Very confused on how to display data in React, can anyone give an example of how to do this correctly?对如何在 React 中显示数据感到非常困惑,谁能举例说明如何正确执行此操作? Searched here, docs, google, still not understanding.在这里搜索,文档,谷歌,仍然不明白。

邮递员结果

import React from 'react';
import Axios from 'axios';
import Rater from 'react-rater';
import './comment-styles.scss';

export class CommentBox extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            reviews: []
        }
        this.addReview = this.addReview.bind(this);
    };


    addReview() {
        Axios({
            method: 'POST',
            url: 'http://localhost:8080/breakfast-tacos/reviews',
            headers: {
                'Content-Type': 'application/json'
            }
        }).then(response => {
            this.setState({ reviews: [response.data.id] })      
        }).catch(error => console.log(error))
    }


    componentDidMount() {

        Axios.get('http://localhost:8080/breakfast-tacos/')
        .then(reviews => {
            this.setState({ reviews: reviews.data })
        })
        .catch(err => console.log(err))
    };



    render() {
        
        return (
            <div id="commentWrapper">
                <div className="commentHeader">
                    <h5>Leave a Rating!</h5>
                    <Rater style={{fontSize: '35px'}} />
                    <button id="submitRatingBtn" type="submit">Submit</button>

                    <form action="/breakfast-tacos" method="POST">
                        <textarea className="reviewTextBox" maxLength="250" placeholder="Write a review..." name="reviews"></textarea>
                        <button id="submitReviewBtn" type="submit" onClick={this.addReview}>Submit</button>
                    </form>

                </div>
                <hr />
                <div className="reviews">
                    <span className="user">
                        <h6>username:</h6>
                    </span>
                    <span className="text">
                        
                        {this.state.reviews.map((review, text) => {  
                                return (
                                    <p key={text}>
                                        {review} /*Where I want to display the data*/
                                    </p>
                                )
                            })}

                    </span>
                </div>
                <hr />
            </div>
        )
    }
};


Response data is an object with property reviews which is an array, so you need to write:响应数据是一个带有属性reviews的 object,它是一个数组,因此您需要编写:

componentDidMount() {

        Axios.get('http://localhost:8080/breakfast-tacos/')
        .then(reviews => {
            this.setState({ reviews: reviews.data.reviews })
        })
        .catch(err => console.log(err))
    };

And in JSX part:在 JSX 部分:

{review.reviews}

because review is array element which is an object.因为review是数组元素,它是一个 object。

You are still in the array.你还在数组中。 In order to map through the array, you need to locate the object properties.为了 map 通过阵列,您需要定位 object 属性。

addReview() {
        Axios({
            method: 'POST',
            url: 'http://localhost:8080/breakfast-tacos/reviews',
            headers: {
                'Content-Type': 'application/json'
            }
        }).then(response => {
            this.setState({ reviews: [response.data.id] })      
        }).catch(error => console.log(error))
    }


    componentDidMount() {

        Axios.get('http://localhost:8080/breakfast-tacos/')
        .then(reviews => {
            this.setState({ reviews: reviews.data.reviews })
        })
        .catch(err => console.log(err))
    };

You should be able to map through the array now without seeing the error.您现在应该能够通过阵列 map 而不会看到错误。

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

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