简体   繁体   English

反应本机 firestore 时间戳

[英]react native firestore timestamps

I'm trying to fetch the data ("createdAt") field from my orders collection.我正在尝试从我的订单集合中获取数据(“createdAt”)字段。 When I called it in my code it gives me this error: "[Unhandled promise rejection: Error: Objects are not valid as a React child (found: object with keys {seconds, nanoseconds}). If you meant to render a collection of children, use an array instead.]"当我在我的代码中调用它时,它给了我这个错误:“[未处理的 promise 拒绝:错误:对象作为 React 子项无效(找到:object,键为 {seconds,nanoseconds})。如果你想渲染一个集合孩子们,请改用数组。]”

 <View style={styles.ViewContainer}>
      {orders.map((order, index) => {
        return (
          <View style={styles.orderCard}>
            {/* <Text>{order.createdAt}</Text> */}
            <View>
              <Text
                style={{
                  color: "black",
                  fontSize: 12,
                  fontFamily: "AvenirNext-Regular",
                  fontWeight: "300",
                  bottom: 4,
                }}
              >
                {order.createdAt}
              </Text>
            </View>
            {order.items.map((item) => (
              <View style={styles.orderItem}>
                <Text style={styles.orderText}>
                  You sent a coffee from {item.cafeName} for {item.price} and 
                </Text>

The issue is that order.createdAt is an object -- a Firestore Timestamp .问题是order.createdAt是一个 object——一个Firestore 时间戳 React doesn't know how to render that object. React 不知道如何渲染 object。

So, convert it to something that React does know how to render, like using the Timestamp.toDate() function of the Timestamp:因此,将其转换为 React 知道如何呈现的内容,例如使用时间戳的Timestamp.toDate() function:

{order.createdAt.toDate()}

Firebase timestamp is object with this format Firebase 时间戳是 object 这种格式

order.createdAt = {nanoseconds: 0,
seconds: 1562524200}



You need to transform second to human-readable format like Sun Jul 07 2019 20:30:00 .您需要将秒转换为人类可读的格式,例如Sun Jul 07 2019 20:30:00

Refactor code as below;重构代码如下;


<Text
  style={{
    color: "black",
    fontSize: 12,
    fontFamily: "AvenirNext-Regular",
    fontWeight: "300",
    bottom: 4,
  }}
>
  {new Date(order.createdAt.seconds * 1000)}
</Text>;

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

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