【发布时间】: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