Javascript firestore 1.0未返回firestore时间戳

Javascript firestore 1.0未返回firestore时间戳,javascript,node.js,firebase,google-cloud-firestore,google-cloud-functions,Javascript,Node.js,Firebase,Google Cloud Firestore,Google Cloud Functions,以前,我的代码运行良好,并从firestore返回时间戳的Long值。自firestore 1.0发布以来,代码返回的结果是[object object]。虽然它将时间戳保存为2018年6月5日UTC+3上午10:38:44时的long,这意味着它当前未保存为firestore数据库中的long值。昨晚我尝试了一些可能的解决办法,但没有奏效。有什么解决办法吗 exports.get\u time=functions.https.onRequest((请求,响应)=>{ //如果(!reques

以前,我的代码运行良好,并从firestore返回时间戳的
Long
值。自firestore 1.0发布以来,代码返回的结果是
[object object]
。虽然它将时间戳保存为2018年6月5日UTC+3上午10:38:44时的
long
,这意味着它当前未保存为firestore数据库中的
long
值。昨晚我尝试了一些可能的解决办法,但没有奏效。有什么解决办法吗

exports.get\u time=functions.https.onRequest((请求,响应)=>{
//如果(!request.headers.authorization){
//错误('没有传递Firebase ID令牌');
//响应。状态(403)。发送(“未授权”);
//返回;
// }
var fieldValue=require(“firebase管理员”).firestore.fieldValue;
db.collection('times').doc('servertime').set({servertime:fieldValue.serverTimestamp()})。然后((ref)=>{
db.collection('times').doc('servertime').get().then((快照)=>{
writeHead(200,{“内容类型”:“text/plain”});
write(字符串(snapshot.data());
response.end();
返回null;
}).catch((错误)=>{
writeHead(404,{“内容类型”:“text/plain”});
写入(“错误\n”+错误);
response.end();
});
返回null;
}).catch((错误)=>{
writeHead(404,{“内容类型”:“text/plain”});
写入(“错误\n”+错误);
response.end();
});

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

在这个版本中,存储在Firestore中的日期对象的行为发生了变化:“存储在Cloud Firestore中的时间戳作为 Firebase时间戳对象,而不是系统日期对象。“

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

  // 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();

Am getting
TypeError:无法读取未定义的属性“toDate”
。“created_at”字段被视为默认字段还是我存储时间戳的字段?请检查上面代码中的我的文档引用,以防我需要在
集合
级别阅读它应该是存储时间戳的字段。如果不清楚,很抱歉。非常感谢,我正在重新尝试代码,请记住。您也可以观看Firebase团队关于HTTP云功能的视频。对于
set()
get()
不需要捕捉。在promises链接的末尾,一个捕获就足够了。很好解决了,我不得不删除第二行,因为它导致了
TypeError:timestamp.toDate不是一个函数
并且返回了时间戳的字符串版本,我现在收到
2018年6月5日星期二09:21:56 GMT+0000(UTC)
作为响应。如果这个问题可能有助于新版本的用户,请考虑进行投票。
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)
        });
});