【发布时间】:2021-09-20 13:51:59
【问题描述】:
我正在尝试获取以正确格式存储在 firebase 中的订单数据,但是当我尝试在 console.log() 中查看时,它显示的数据格式不正确。
这是我从 firebase 收到的数据。
Array [
Order {
"date": 1200,
"id": "-Me6W4SzHB3RbV_3PdcO",
"items": "u1",
"totalAmount": Array [
Object {
"productId": "-Md76UnJ1I1lNhGgldH3",
"productPrice": 1200,
"productTitle": "Macbook Air M1 2020",
"quantity": 1,
"sum": 1200,
},
],
},
Order {
"date": 1200,
"id": "-Me92VJEha-d-x5S3oya",
"items": "u1",
"totalAmount": Array [
Object {
"productId": "-Md76UnJ1I1lNhGgldH3",
"productPrice": 1200,
"productTitle": "Macbook Air M1 2020",
"quantity": 1,
"sum": 1200,
},
],
},
Order {
"date": 1200,
"id": "-Me93LLXuho2T0D1Vpqx",
"items": "u1",
"totalAmount": Array [
Object {
"productId": "-Md76UnJ1I1lNhGgldH3",
"productPrice": 1200,
"productTitle": "Macbook Air M1 2020",
"quantity": 1,
"sum": 1200,
},
],
},
]
而且,当我尝试控制台记录我从动作/订单收到的订单时,我得到了这个,我正在调度一个动作以从 firebase 获取数据。
export const getOrders = () => {
return async (dispatch) => {
try {
const response = await fetch(
"https://shopping-app-62***-default-rtdb.firebaseio.com/orders/u1.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,
"u1",
resData[key].cartItems,
resData[key].totalAmount,
new Date(resData[key].date)
)
);
}
console.log(loadedOrders);
dispatch({ type: GET_ORDER, orders: loadedOrders });
} catch (error) {
throw error;
}
};
};
订单类:
import moment from "moment";
class Order {
constructor(id, items, totalAmount, date) {
this.id = id;
this.items = items;
this.totalAmount = totalAmount;
this.date = date;
}
get readableDate() {
return moment(this.date).format("MMMM Do YYYY, hh:mm");
}
}
export default Order;
我附上了下面的图片,以显示我的数据是如何存储在 firebase 中的。
为什么我在 totalAmount 中得到一个产品详细信息数组,而不是 date 我得到的是 totalAmount?
【问题讨论】:
-
请在您的问题中提供您的
Order班级。 -
@samthecodingman 当然,我已经更新了我的问题。
标签: javascript react-native firebase-realtime-database react-redux fetch