我在彈出的 expo 應用程式中使用 react-native-firebase 并嘗試在我的聊天應用程式中構建一個存在檢測系統,該系統將檢測訊息收件人是否在線以及他/她上次在線的時間。資料將按如下方式存盤在 firebase 實時資料庫中:
{
lastSeen:[{
[userId]:{
state: boolean
time: serverTimeStamp
}
}]
}
問題是 firebase 控制臺從不顯示資料,只有當收件人在線時,應用程式才會顯示此資料(即使它在控制臺中不可見),但如果用戶離線,則不會回傳任何內容并且不會生成錯誤。我已在 realtimeDB 規則中將讀取和寫入設定為 true。這是我正在使用的代碼:
import database from "@react-native-firebase/database";
export const updateUserLastSeen = (userId) => {
const userStatusDatabaseRef = database().ref("/lastSeen/" userId);
console.log("updatelast", userId);
userStatusDatabaseRef
.set({
state: true,
time: database.ServerValue.TIMESTAMP,
})
.then(() => console.log("online"))
.catch((e) => console.log(e));
// database()
// .ref(".info/connected")
// .on("value", function (snapshot) {
// if (snapshot.val() == false) {
// return;
// }
userStatusDatabaseRef
.onDisconnect()
.set({
state: false,
time: database.ServerValue.TIMESTAMP,
})
.then(function () {
console.log("disconnect configured");
// userStatusDatabaseRef.set({
// state: true,
// time: database.ServerValue.TIMESTAMP,
// });
});
// });
};
export const checkUserLastSeen = (userId, setUserLastSeen) => {
console.log("check last", userId);
database()
.ref("/lastSeen/" userId)
.on("value", (snapshot) => {
setUserLastSeen(snapshot.val());
console.log("User data: ", snapshot.val());
});
console.log("after check last");
};
I tried both the code from firebase docs and rnfirebase docs. In above code, none of the "then" or "catch" functions get called in updateUserLastSeen but in checkUserLastSeen "on" is invoked only if bearer of userId is online. Also, I am using realtime db only for this purpose and cloud firestore for other data storing and its working fine. Any help would be appreciated. Thanks.
uj5u.com熱心網友回復:
如果既不呼叫then也不catch呼叫寫入,則通常意味著客戶端未連接到服務器。
我建議檢查以確保您的應用程式具有網路連接,并且您已為您的資料庫配置了(正確的)URL。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/310811.html
標籤:javascript firebase-realtime-database react-native-firebase
