一个自定义Object对象

一个自定义Object对象

本文介绍了Firebase Firestore-数据必须是一个对象,但是它是:一个自定义Object对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Firestore与实时数据库结合使用,以便为我的应用程序提供用户状态系统.

I'm using Firestore in conjunction with realtime database in order to provide a user presence system for my application.

更新:我关注的文章

https://blog.campvanilla.com/firebase-firestore-guide-how-to-user-presence-online-offline-basics-66dc27f67802

在其中一种方法中,我在此处使用此代码:

In one of the methods I use this code here:

const usersRef = this.$fireStore.collection('users').doc(uid)
const whoIsOnlineRef = this.$fireDb.ref('.info/connected')

whoIsOnlineRef.on('value', (snapshot) => {
    this.$fireDb.ref(`/status/${uid}`)
        .onDisconnect()
        .set('offline')
        .then(() => {
          usersRef.set({ online: true }, { merge: true })

          this.$fireDb.ref(`/status/${uid}`).set('online')
        })
})

但是,.set方法给了我标题中提到的错误,我不太明白为什么.我只是将一个javascript对象传递给.set方法,这在技术上应该可以正常工作.

The .set method, however, is giving me the error mentioned in the title and I can't quite understand why. I'm simply passing a javascript object to .set method and this should technically work.

您能发现代码有什么问题吗?

Can you spot what is wrong with the code?

推荐答案

看起来不起作用的原因是代码在SSR应用程序的服务器上运行.

Looks like the reason why this wasn't working is that the code was running on the server in an SSR application.

我将相同的逻辑移至浏览器,并且开始很好地工作.我仍然不明白为什么这在服务器中不起作用,因为直到最后,我仍然将一个简单的js对象传递给.set()方法.

I moved that very same logic to the browser and it started working nicely. I still don't see why this wouldn't work in the server as, at the end of the day I was still passing a simple js object to the .set() method.

无论哪种方式,您都可以拥有

Either way, there you have it

这篇关于Firebase Firestore-数据必须是一个对象,但是它是:一个自定义Object对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 12:19