Firebase V9 在线状态
Firebase V9 Online Status
我正在尝试在 React 中将一些 Firebase v8 代码重新配置为 firebase v9...在 login/register 页面上,我想将在线状态更改为 true。
我不确定这行代码与更新后的变量名是否会有很大不同或完全相同。这可能是一个非常简单的问题(希望如此)。我知道在 v9 中,很多这些类型的东西都通过 firebase 库中已有的函数的导入语句进行了重组。 IDK 如果这是其中之一......:/。谢谢你的时间!如果我需要包含更多代码才能有意义,请提前告诉我...
旧(工作)v8 代码:
变量:
- projectAuth = firebase.auth()
- projectFirestore = firebase.firestore()
关注下面一行:“// set online to be true”
...在登录/注册页面中:
try {
// login
const res = await projectAuth.signInWithEmailAndPassword(email, password)
//set online to be true
await projectFirestore.collection('users').doc(res.user.uid)
.update({ online: true })
// dispatch login action
dispatch({ type: 'LOGIN', payload: res.user })
if (!isCancelled) {
setIsPending(false)
setError(null)
}
}
我的 v9 代码:
变量:
- auth = getAuth()
- db = getFirestore()
try {
// login
await signInWithEmailAndPassword(auth, email, password)
.then((res) => {
// dispatch login action
dispatch({ type: 'LOGIN', payload: res.user })
//set online to be true
await projectFirestore.collection('users').doc(res.user.uid)
.update({ online: true })
if (!isCancelled) {
setIsPending(false)
setError(null)
}
})
有一个顶级函数updateDoc()
来更新文档:
import { updateDoc, doc } from "firebase/firestore";
const docRef = doc(db, "users", res.user.uid)
await updateDoc(docRef, { online: true });
我正在尝试在 React 中将一些 Firebase v8 代码重新配置为 firebase v9...在 login/register 页面上,我想将在线状态更改为 true。
我不确定这行代码与更新后的变量名是否会有很大不同或完全相同。这可能是一个非常简单的问题(希望如此)。我知道在 v9 中,很多这些类型的东西都通过 firebase 库中已有的函数的导入语句进行了重组。 IDK 如果这是其中之一......:/。谢谢你的时间!如果我需要包含更多代码才能有意义,请提前告诉我...
旧(工作)v8 代码:
变量:
- projectAuth = firebase.auth()
- projectFirestore = firebase.firestore()
关注下面一行:“// set online to be true” ...在登录/注册页面中:
try {
// login
const res = await projectAuth.signInWithEmailAndPassword(email, password)
//set online to be true
await projectFirestore.collection('users').doc(res.user.uid)
.update({ online: true })
// dispatch login action
dispatch({ type: 'LOGIN', payload: res.user })
if (!isCancelled) {
setIsPending(false)
setError(null)
}
}
我的 v9 代码:
变量:
- auth = getAuth()
- db = getFirestore()
try {
// login
await signInWithEmailAndPassword(auth, email, password)
.then((res) => {
// dispatch login action
dispatch({ type: 'LOGIN', payload: res.user })
//set online to be true
await projectFirestore.collection('users').doc(res.user.uid)
.update({ online: true })
if (!isCancelled) {
setIsPending(false)
setError(null)
}
})
有一个顶级函数updateDoc()
来更新文档:
import { updateDoc, doc } from "firebase/firestore";
const docRef = doc(db, "users", res.user.uid)
await updateDoc(docRef, { online: true });