使用 Firebase 和 Firestore 构建存在,部署函数中出现错误:“秒不是有效整数”

BB *_*ign 3 javascript firebase firebase-realtime-database google-cloud-firestore

我正在使用Firebase 提供的此文档,通过使用 Firebase 的内置状态和onDisconnect() ,结合使用 Firebase 和 Firestore 在 Firestore 中建立“状态” 。大部分代码都是 Firebase 提供的逐字记录,但我为我的数据结构定制了一些内容。我已成功将我的函数(如下)部署到 Firebase。检查日志,我收到此错误:

Error: Value for argument "seconds" is not a valid integer.
    at Object.validateInteger (/srv/node_modules/@google-cloud/firestore/build/src/validate.js:191:19)
    at new Timestamp (/srv/node_modules/@google-cloud/firestore/build/src/timestamp.js:70:20)
    at Function.fromMillis (/srv/node_modules/@google-cloud/firestore/build/src/timestamp.js:126:16)
    at Function.fromDate (/srv/node_modules/@google-cloud/firestore/build/src/timestamp.js:108:26)
    at Serializer.encodeValue (/srv/node_modules/@google-cloud/firestore/build/src/serializer.js:97:53)
    at Serializer.encodeFields (/srv/node_modules/@google-cloud/firestore/build/src/serializer.js:56:30)
    at Function.fromObject (/srv/node_modules/@google-cloud/firestore/build/src/document.js:97:53)
    at op (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:245:58)
    at writes._ops.map.op (/srv/node_modules/@google-cloud/firestore/build/src/write-batch.js:431:44)
    at Array.map (<anonymous>)
Run Code Online (Sandbox Code Playgroud)

由于我主要使用 Firebase 提供的代码,所以我真的不知道如何调试它。我猜这与 Firebase 和 Firestore 之间的时间戳比较有关。关于这可能出问题的地方有什么建议吗?

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

const firestore=admin.firestore();

exports.onUserStatusChanged=functions.database.ref('user/{uid}').onUpdate(
    async (change, context) => {
        const eventStatus=change.after.val();
        const userStatusFirestoreRef=firestore.doc(`user/${context.params.uid}`);
        const statusSnapshot=await change.after.ref.once('value');
        const status=statusSnapshot.val();
        console.log(status, eventStatus);

        if (status.last_changed>eventStatus.last_changed){
            return null;
        }

        eventStatus.last_changed=new Date(eventStatus.last_changed);

        return userStatusFirestoreRef.set(eventStatus);
    }
);
Run Code Online (Sandbox Code Playgroud)

dan*_*-sc 7

如果您尝试保留无效日期,也会发生此错误,例如:

new Date("string that cannot be parsed as timestamp/date")
Run Code Online (Sandbox Code Playgroud)