Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 处理用户’;Firebase中的联机和脱机状态_Javascript_Firebase_Firebase Realtime Database_Google Cloud Functions - Fatal编程技术网

Javascript 处理用户’;Firebase中的联机和脱机状态

Javascript 处理用户’;Firebase中的联机和脱机状态,javascript,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,我想在我的webapp中处理联机和脱机状态。 这样用户就可以看到谁在线,谁不在线 我发现这个很棒的教程解释得很好,但我被卡住了 我发现云函数有问题,因为我在那里出错了 此外,本教程是2017年12月15日开始的,我知道云函数已更新,但我不知道如何更新代码 链接到文档: 有人可以看一下教程,也许可以帮我一下吗 云功能: const functions = require('firebase-functions'); const Firestore = require('@google-clo

我想在我的webapp中处理联机和脱机状态。 这样用户就可以看到谁在线,谁不在线

我发现这个很棒的教程解释得很好,但我被卡住了

我发现
云函数有问题,因为我在那里出错了

此外,本教程是2017年12月15日开始的,我知道
云函数已更新,但我不知道如何更新代码

链接到文档:

有人可以看一下教程,也许可以帮我一下吗

云功能:

 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
      })
  });

我将发布完整的功能代码,以帮助像我一样坚持使用它的其他人

const functions=require('firebase-functions');
const admin=require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const firestore=functions.firestore;
exports.onUserStatusChange=functions.database
.ref('/status/{userId}'))
.onUpdate((事件、上下文)=>{
var db=admin.firestore();
var fieldValue=require(“firebase管理员”).firestore.fieldValue;
const usersRef=db.collection(“用户”);
var snapShot=event.after;
返回事件.after.ref.once('value')
.then(statusSnap=>snapShot.val())
。然后(状态=>{
如果(状态==‘脱机’){
usersRef
.doc(context.params.userId)
.设置({
在线:错误
},{merge:true});
}
返回null;
})
});

Firestore官方文档()中有一个更新的教程,解释了如何设置Firestore、实时数据库和云功能。

使用您的代码,我最终可以部署,但无法工作<代码>状态==='offline'
Firebase是否脱机,而不是Firestore?是的,它将获得Firebase状态,然后将其传递给Firestore。我想我的问题是我没有在我的Flitter应用程序上持续检查用户状态。。。需要看一个好的教程吗