我正在嘗試在 React 中將一些 Firebase v8 代碼重新配置為 firebase v9 ...在登錄/注冊頁面上,我想將在線狀態更改為 true。
我不確定這行代碼是否會與更新的變數名有很大不同或完全相同。這可能是一個非常簡單的問題(希望如此)。我知道在 v9 中,很多這些型別的東西都是用 firebase 庫中已經可用的函式的匯入陳述句進行重組的。IDK,如果這是其中之一……:/。謝謝你的時間!如果我需要包含更多代碼以使其有意義,請提前告訴我...
舊(作業)v8 代碼:
變數:
- projectAuth = firebase.auth()
- projectFirestore = firebase.firestore()
關注下面的行:“//設定在線為真”......在登錄/注冊頁面中:
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 代碼:
變數:
- 身份驗證= 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)
}
})
uj5u.com熱心網友回復:
updateDoc()有一個更新檔案的頂級功能:
import { updateDoc, doc } from "firebase/firestore";
const docRef = doc(db, "users", res.user.uid)
await updateDoc(docRef, { online: true });
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/482096.html
標籤:javascript 反应 火力基地 谷歌云火库
