【问题标题】:Format of Data is incorrect when fetching and receiving it from firebase in react native app在 react native 应用程序中从 firebase 获取和接收数据时,数据格式不正确
【发布时间】: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


【解决方案1】:

您正在这里创建 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;
}

【讨论】:

  • 是的,由于 'u1' 它无法正常工作。现在我正在以正确的格式获取数据。非常感谢您的帮助。
猜你喜欢
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 2020-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
相关资源
最近更新 更多