我希望在我的Web应用程序中处理在线和离线状态,以便用户可以看到谁在线,谁不在线。
我发现了这个很好的教程,它解释得非常好,但我卡住了。
链接:https://blog.campvanilla.com/firebase-firestore-guide-how-to-user-presence-online-offline-basics-66dc27f67802 我认为
此外,该教程是2017年12月发布的,我知道
文档链接:https://firebase.google.com/docs/functions/beta-v1-diff 有人能否查看一下这个教程,并帮我解决问题吗?
云函数:
我发现了这个很好的教程,它解释得非常好,但我卡住了。
链接:https://blog.campvanilla.com/firebase-firestore-guide-how-to-user-presence-online-offline-basics-66dc27f67802 我认为
cloud-functions
存在问题,因为我在那里遇到了一个错误。此外,该教程是2017年12月发布的,我知道
cloud-functions
已经更新了,但我不知道如何更新代码。文档链接:https://firebase.google.com/docs/functions/beta-v1-diff 有人能否查看一下这个教程,并帮我解决问题吗?
云函数:
const functions = require('firebase-functions');
const Firestore = require('@google-cloud/firestore');
const firestore = new Firestore();
exports.onUserStatusChanged = functions.database
.ref('/status/{userId}') // Reference to the Firebase RealTime database key
.onUpdate((event, context) => {
const usersRef = firestore.collection('/users'); // Create a reference to
the Firestore Collection
return event.before.ref.once('value')
.then(statusSnapshot => snapShot.val()) // Get latest value from the Firebase Realtime database
.then(status => {
// check if the value is 'offline'
if (status === 'offline') {
// Set the Firestore's document's online value to false
usersRef
.doc(event.params.userId)
.set({
online: false
}, {
merge: true
});
}
return
})
});