错误:对象无效,不能作为反应子对象

时间:2020-05-18 17:50:42

标签: javascript reactjs react-hooks

我正在尝试遍历数据库中的数据,但是反应给了我这个错误 enter image description here

我尝试通过在单独的组件中呈现列表仍然不起作用!,我是生态系统的初学者。 提取的数据看起来像- enter image description here

我的页面代码是这个

import React, { useEffect, useState } from 'react';
import './account.scss';
import { connect } from 'react-redux';
import { createStructuredSelector } from 'reselect';
import { selectCurrentUser } from '../../reducer/user/user.selectors';
import Order from '../../models/order';


const AccountPage = ({ currentUser }) =>{
const[orders,setOrders]=useState([]);

useEffect(()=>{
    const fetchFunc= async () =>{
        try{
            const response= await  fetch(`https://orders-6exxx.firebaseio.com/userorders/${currentUser.id}.json`);

            if(!response.ok){
                throw new Error('Something Went Wrong!');
            }
            const resData= await response.json();
            const loadedOrders=[];

            for(const key in resData){
                loadedOrders.push(
                    new Order(
                  key,
                  resData[key].cartItems,
                  resData[key].totalAmount,
                  new Date(resData[key].date)
                ));
            }
       setOrders(loadedOrders);
        }catch(err){
     throw err;
        }
    };

    fetchFunc();


},[currentUser]);

console.log(orders);

    return(
        <div className='account'>
        {orders&&orders.length ? (
            <div className='account__container'>



              {
                 orders.map(order =>(
                     <div className='account__containers__orders'>
                         <div className='account__containers__orders--date'>
                               {order.date}
                             </div>

                             {
                                 order.items.map(item => (
                                     <div className='account__containers__orders__item'>
                                          <div>{item.name}</div>
                                          <div>{item.quantity} x</div>
                                          <div>{item.price}</div>
                                     </div>
                                 ))
                             }

                          <div className='account__containers__orders--total'>
                              <span className='price--tag'>&#8377;</span> {order.totalAmount}
                             </div>

                </div>
                 ))
             }      

    </div>
        ) : (
            <div className='orders-loading'>
                No Orders
            </div>
        )}

   </div>
    );
};



const mapStateToProps = createStructuredSelector({
    currentUser: selectCurrentUser
  });



  export default connect(
   mapStateToProps
  )(AccountPage);

单个数据正在加载,但是当我尝试打印多个数据时,它给了我这个错误

1 个答案:

答案 0 :(得分:2)

您将& aws configure | Write-host存储在订单状态内,并且将其视为对象。为了呈现它,您可以将其转换为字符串

new Date()

{order.date.toString()}