【问题标题】:Building presence with Firebase and Firestore, error in deployed function: "seconds is not a valid integer"使用 Firebase 和 Firestore 构建存在,部署函数中的错误:“秒不是有效整数”
【发布时间】:2020-07-30 03:22:41
【问题描述】:

我正在使用 Firebase 提供的this document,结合使用 Firebase 和 Firestore,通过使用 Firebase 的内置状态和 onDisconnect() 在 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>)

由于我主要使用 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);
    }
);

【问题讨论】:

  • 这里有一个线索。我尝试添加 console.log 语句来获取 status.last_changed 和 eventStatus.last_changed 的​​值,但它们都是“未定义的”。所以这是有道理的。那么为什么 Firebase 自己的代码包含对未定义内容的引用?
  • 看起来必须在这些对象上预先定义,在此脚本运行之前。
  • 我在两个位置都添加了时间戳值,现在错误已经消失了。

标签: javascript firebase firebase-realtime-database google-cloud-firestore


【解决方案1】:

解决方案是需要在它们被引用的对象中预定义 last_changed 元素。在这种情况下它们丢失了,导致错误。

【讨论】:

    【解决方案2】:

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

    new Date("string that cannot be parsed as timestamp/date")
    

    【讨论】:

      猜你喜欢
      • 2020-08-18
      • 1970-01-01
      • 2018-07-03
      • 2021-05-03
      • 2017-12-12
      • 2018-09-13
      • 1970-01-01
      • 2018-08-04
      • 2021-05-06
      相关资源
      最近更新 更多