我正在制作一個使用 Firebase 作為后端即服務的移動應用程式。我正在使用 Firebase 的 Firebase SDK 身份驗證和 Cloud Firestore。
我的問題是我希望用戶僅在登錄時才能看到他們自己的資料。我看到您可以在資料庫集合中使用您的用戶 ID,然后檔案 ID 將是用戶 ID。
這是我的代碼看起來有多遠,但它給了我關于 db.collection 不是函式的錯誤:
import { StyleSheet, Text, View, TextInput, Button } from 'react-native'
import React, {useState} from 'react'
import { auth, db } from '../../firebase/firebase'
import { collection, doc} from 'firebase/firestore';
export default function CreateUserComponent() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [username, setUsername] = useState('');
const [firstname, setFirstname] = useState('');
const [lastname, setLastname] = useState('');
const usersCollectionRef = collection(db, "users");
const handleSignUp = () => {
auth
.createUserWithEmailAndPassword(email, password)
.then(userCredentials => {
userCredentials.user;
})
.catch(error => alert(error.message))
};
const createUserData = async () => {
auth
.onAuthStateChanged(user => {
usersCollectionRef.doc(user.uid).set({
email: email,
username: username,
firstname: firstname,
lastname: lastname,
password: password
})
});
}
const handleSignUpAndPostData = () => {
handleSignUp();
createUserData();
};
return (
<View style={styles.view}>
<View>
<Text style={styles.container}>CreateUserComponent: Welcome to your
registration</Text>
</View>
<View>
<Text>E-mail:</Text>
<TextInput style={styles.input} placeholder="Enter your email" value={email}
onChangeText={text => setEmail(text)}></TextInput>
<Text>Username:</Text>
<TextInput style={styles.input} placeholder="Enter your username" value=
{username} onChangeText={text => setUsername(text)}></TextInput>
<Text>Firstname:</Text>
<TextInput style={styles.input} placeholder="Enter your firstname" value=
{firstname} onChangeText={text => setFirstname(text)}></TextInput>
<Text>Lastname:</Text>
<TextInput style={styles.input} placeholder="Enter your lastname" value=
{lastname} onChangeText={text => setLastname(text)} ></TextInput>
<Text>Password:</Text>
<TextInput style={styles.input} secureTextEntry placeholder="Enter your
password" value={password} onChangeText={text => setPassword(text)}>
</TextInput>
</View>
<View style={styles.button}>
<Button onPress={handleSignUpAndPostData} title= "Continue"/>
</View>
</View>
)
}
const styles = StyleSheet.create({
container: {
fontSize: 16,
fontWeight: 'bold',
color: 'dodgerblue',
marginTop: 100,
padding: 20
},
view: {
alignItems: 'center',
justifyContent: 'center',
},
input: {
borderWidth: 1,
borderColor: '#777',
padding: 8,
margin: 10,
width: 200,
backgroundColor: 'white'
},
button: {
marginTop: 20,
width: "30%",
}
})
這是它給我的錯誤訊息: usersCollectionRef.doc is not a function UsersCollectionRef.doc(user.uid)
uj5u.com熱心網友回復:
根據您所附的螢屏截圖,如果您使用的是模塊化 Firebase 版本,doc則不存在正確的方法。usersCollectionRef
從Firestore 檔案開始。對于您的用例,它將如下所示:
import { doc, setDoc } from "firebase/firestore";
// ...
setDoc(doc(db, `users/${user.uid}`), {
// your data you want to write to Firestore
});
uj5u.com熱心網友回復:
回答
嘿 -
- 是的,使用用戶的
uid來自 firebase 身份驗證是一個很棒的系統 - 查詢用戶檔案的最佳實踐是將用戶
uid作為欄位添加到他們的檔案中。這意味著uid在您的用戶集合上添加一個與用戶 auth().currentUser.uid 匹配的屬性。
一些基本原理和例子
這使您可以輕松地僅查詢特定用戶的檔案。
例如,假設您有一個筆記應用程式,其中包含用于用戶組態檔的“Users”集合和用于將每個筆記存盤在其自己的檔案中的“Notes”集合。
“Users”集合中的uid每個檔案都有一個屬性,“Notes”集合中的每個檔案也都有一個uid屬性。
查詢您查詢的用戶個人資料Users.where("uid", ===, auth().currentUser.uid).limit(1)
要查詢該用戶的所有筆記,您可以執行Notes.where("uid", ===, auth().currentUser.uid).
此外,如果您在應用程式中有筆記檔案夾,您需要為每個筆記添加一個 folderId。
Firestore 針對非常大且扁平的檔案集合進行了優化。您只需要確保將所有正確的 id 添加到檔案中,以便您可以正確查詢它們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/453347.html
標籤:javascript 火力基地 反应式 谷歌云火库
上一篇:在firebase中獲取多個資料
