【问题标题】:firestore 1.0 not returning Firestore timestampfirestore 1.0 不返回 Firestore 时间戳
【发布时间】:2018-06-05 07:53:08
【问题描述】:

之前,我的代码运行良好,并从 firestore 返回 Long 时间戳值。自 firestore 1.0 发布以来,代码返回 [object Object] 作为结果。尽管它将时间戳保存为June 5, 2018 at 10:38:44 AM UTC+3,这意味着它当前未在firestore 数据库中保存为long 值。我从昨晚开始尝试了一些可能的解决方案,但没有奏效。有什么解决办法吗?

exports.get_time = functions.https.onRequest((request, response) => {
    // if (!request.headers.authorization) {
    //     console.error('No Firebase ID token was passed');
    //     response.status(403).send('Unauthorized');
    //     return;
    // }
    var fieldValue = require("firebase-admin").firestore.FieldValue;
    db.collection('times').doc('servertime').set({servertime: fieldValue.serverTimestamp()}).then((ref) => {
        db.collection('times').doc('servertime').get().then((snapshot) => {
            response.writeHead(200, {"Content-Type": "text/plain"});
            response.write(String(snapshot.data()));
            response.end();
            return null;
        }).catch((error) => {
            response.writeHead(404, {"Content-Type": "text/plain"});
            response.write("Error\n" + error);
            response.end();
        });
        return null;
    }).catch((error) => {
        response.writeHead(404, {"Content-Type": "text/plain"});
        response.write("Error\n" + error);
        response.end();
    });
});

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您的问题可能来自 Firestore SDK 最新版本(JavaScript SDK 版本 5.0.3)中引入的更改:

    在此版本中,存储在 Firestore 中的 Date 对象的行为发生了变化:“存储在 Cloud Firestore 中的时间戳被读回为 Firebase Timestamp 对象而不是系统 Date 对象。"

    你需要“更新你的代码期望一个日期而不是一个时间戳”,如下所示:

      // Old:
      const date = snapshot.get('created_at');  // <- 'created_at' is an example of field name
    
      // New:
      const timestamp = snapshot.get('created_at');
      const date = timestamp.toDate();
    

    此外,您应该以不同的方式链接您的 Promise,并更改发送响应的方式。您可以观看以下视频,了解有关如何编写 HTTP 函数的更多详细信息:https://www.youtube.com/watch?v=7IkUgCLr5oA

    exports.get_time = functions.https.onRequest((request, response) => {
    
        var fieldValue = require("firebase-admin").firestore.FieldValue;
    
        db.collection('times').doc('servertime').set({servertime: fieldValue.serverTimestamp()})
            .then((ref) => {
    
                return db.collection('times').doc('servertime').get();
    
            })
            .then((snapshot) => {
                response.send(snapshot.data());
                // return null;  no need to return here since it is an HTTP Function
            })
            .catch((error) => {
                response.status(500).send(error)
            });
    }); 
    

    【讨论】:

    • 收到TypeError: Cannot read property 'toDate' of undefined。 'created_at' 字段是否被视为默认字段或我存储时间戳的字段?请在上面的代码中查看我的文档参考,以防我需要在Collection 级别阅读
    • 它应该是您存储时间戳的字段。抱歉,如果不清楚。
    • 感谢,考虑到这一点,我再次尝试代码
    • 您还可以观看 Firebase 团队关于 HTTP 云函数的视频。 youtube.com/…。您不需要为set()get() 抓包。在 Promise 链的末尾,一个 catch 就足够了。
    • 很好解决,我不得不删除第二行,因为它导致TypeError: timestamp.toDate is not a function 并返回时间戳的字符串版本,我现在收到Tue Jun 05 2018 09:21:56 GMT+0000 (UTC) 作为响应。如果这个问题可能对新版本的用户有所帮助,请考虑对其进行投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 2021-12-01
    • 2020-07-31
    相关资源
    最近更新 更多