首页 > 解决方案 > 为什么我的时间戳字段在将数据嵌入到地图列表后变成时间图?

问题描述

我尝试将一些事件文档嵌入到用户文档中名为upcomingEvents(地图列表)的字段中。

所以首先,我查询将像这样嵌入到用户文档中的事件文档

const willStartSoonEventsSnapshot = await db.collection('events')
        .where("createdBy","==", event.createdBy)
        .where("isActive","==", true)
        .where("hasBeenApproved","==", true)
        .where("dateTimeStart",">",now)
        .limit(7)
        .orderBy("dateTimeStart","asc")
        .get()

然后我upcomingEvents使用下面的代码更新用户文档中的字段

        const upcomingEvents = []

        willStartSoonEventsSnapshot.forEach( doc => {
            upcomingEvents.push(doc.data())
        })

        // trying to log createdAt field
        console.log(upcomingEvents[0].createdAt)


        db.doc(`users/${event.createdBy}`).update({
            upcomingEvents
        })

从下图中可以看出,最初在events集合中,createdAt字段是时间戳数据类型

在此处输入图像描述

但是在我运行上面的代码并且数据已成功嵌入upcomingEvents字段中的用户文档(地图列表)之后,createdAt 的时间将在地图中,而不是像这样的时间戳

在此处输入图像描述

正如您从上面的代码中看到的那样,我尝试记录 createdAt 时间戳,这是结果。有数据是时间戳数据类型 在此处输入图像描述

我正在使用 firebase 云功能来运行上面的代码

const admin = require("firebase-admin")
const functions = require('firebase-functions')
admin.initializeApp(functions.config().firebase)

那么,为什么我的时间戳字段会变成这样的地图?我可以将它保存在时间戳数据类型中吗?

标签: node.jsfirebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


从您提供的数据来看,日期字段的属性似乎丢失了。如果正在对数据进行字符串化和解析,请检查代码。因为如果记录中有一个日期字段,你打印它,你会看到它是这样的。

{dateField:{纳秒:5651654613,秒:21356883213}}

那是从 firestore 的查询结果中收到的日期。如果属性丢失,则 Firestore 不会将其视为日期对象。


推荐阅读