在react native应用程序中从firebase获取和接收数据时,数据格式不正确

3zwjbxry  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(342)

我正在尝试获取存储在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,
      },
    ],
  },
]

当我尝试console.log我从action/orders收到的命令时,我得到了这个消息,我在这里发送了一个从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中的一系列产品详细信息,而不是日期?

bvn4nwqk

bvn4nwqk1#

这里您正在创建 Order 物体:

new Order(
  key,
  "u1",
  resData[key].cartItems,
  resData[key].totalAmount,
  new Date(resData[key].date)
)

但它与构造函数声明不一致:

constructor(id, items, totalAmount, date) {
  this.id = id;
  this.items = items;
  this.totalAmount = totalAmount;
  this.date = date;
}

这个 "u1" 价值是不合适的。

constructor(id, unknown, items, totalAmount, date) {
  this.id = id;
  this.items = items;
  this.totalAmount = totalAmount;
  this.date = date;
  this.unknown = unknown;
}

您应该将属性对象传递给构造函数和/或使用类似于jsdoc或typescript的工具来捕获这些问题。

new Order({
  id: key,
  unknown: "u1",
  items: resData[key].cartItems,
  totalAmount: resData[key].totalAmount,
  date: new Date(resData[key].date)
})

constructor({ id, items, totalAmount, date, unknown }) {
  this.id = id;
  this.items = items;
  this.totalAmount = totalAmount;
  this.date = date;
  this.unknown = unknown;
}

相关问题