大佬教程收集整理的这篇文章主要介绍了Firebase Firestore onSnapshot 在使用 Firestore FieldValue for Timestamp 时为空,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想将我的 firestore 时间用于我的聊天应用程序。我编写了下面的代码,但 firestore 字段值存在问题。
我将消息发送到 firestore,例如:
chatsRef.add({
...m,createdAt: firebase.firestore.FIEldValue.servertimestamp(),});
一切正常,但是当我听 (onSnapshot) 引用 chatsRef
的文档时,createdAt
值也总是在我检查 firestore 正确设置的时间时为空。我认为那里有一个异步事件,我必须等待它。我该如何解决这个问题?
chatsRef
:
const chatsRef= db.collection("ChatRooms").doc(ID).collection("Chat")
我用那个代码听了 chatsRef:
useEffect(() => {
let x = null;
const unsubscribe = chatsRef
.orderBy("createdAt","desc")
.limit(10)
.onSnapshot((querySnapshot) => {
const messagesFirestore = querySnapshot
.docChanges()
.filter(({ type }) => type === "added")
.map(({ doc }) => {
const message = doc.data();
x = message;
console.log(X);// I checked my code and the object's createdAt fIEld gets null.
return { ...message,createdAt: message.createdAt.toDate() };
});
appendmessages(messagesFirestorE);
// Some irrelevant codes
return () => unsubscribe();
},[]);
另外我的消息 JsON 结构是:
Object {
"_ID": "a51d2e86-df8d-449e-b41e-73fbd746856f","createdAt": t {
"nanoseconds": 26000000,"seconds": 1616336752
},"text": "1111","user": Object {
"_ID": "jack1212312","name": "jackAndME",},}
最后更改:
useEffect(() => {
let x = null;
const unsubscribe = chatsRef
.orderBy("createdAt","desc")
.limit(10)
.onSnapshot({ includeMetadataChanges: true },(querySnapshot) => {
const messagesFirestore = querySnapshot
.docChanges()
.filter(({ type }) => type === "added")
.map(({ doc }) => {
const message = doc.data();
if (!doc.Metadata.hasPendingWrites) {
x = message;
return { ...message,createdAt: message.createdAt.toDate() };
}
return null;
});
if (messagesFirestore[0]) {
appendmessages(messagesFirestorE);
if (latestmessage.current != null) {
if (
new Date(
latestmessage.current["createdAt"]["seconds"] * 1000 +
latestmessage.current["createdAt"]["nanoseconds"] / 1000000
) >
new Date(
x["createdAt"]["seconds"] * 1000 +
x["createdAt"]["nanoseconds"] / 1000000
)
) {
latestmessage.current = x;
}
} else {
latestmessage.current = x;
}
}
});
return () => unsubscribe();
},[]);
注意:现在,我可以检测元数据,但无法使用快照访问数据的最新版本。
Firebase 文档说:
1- A change event is immediately fired with the new data. The document has not yet been written to the BACkend so the "pending writes" flag is true.
2- The document is written to the BACkend.
3- The BACkend notifIEs the clIEnt of the successful write. There is no change to the document data,but there is a Metadata change because the "pending writes" flag is Now false.
但是当元数据写入后端而不刷新页面快照未获取数据时,我无法访问pendingwrites标志。
这是由于 Firebase 调用的 "latency compensation":
您应用中的本地写入将立即调用快照侦听器。 这是因为一个称为“延迟补偿”的重要功能。 当您执行写入时,您的听众将收到新的通知 数据发送到后端之前的数据。
你的快照是在离开你的应用之前触发的,时间戳字段为null
,然后在到达后端并设置时间戳后再次触发。
要区分两者,请使用文档中所述的字段 @H_590_7@metadata.hasPendingWrites。
以上是大佬教程为你收集整理的Firebase Firestore onSnapshot 在使用 Firestore FieldValue for Timestamp 时为空全部内容,希望文章能够帮你解决Firebase Firestore onSnapshot 在使用 Firestore FieldValue for Timestamp 时为空所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。