使用 firebase auth 正确创建的帐户,但我对 fire store 有问题,我认为这是功能导致我收到此错误:firebaseconfig.firestoreDB.collection 不是一个函数(它是未定义的)。
当我询问聊天 gpt 时,它一直给我同样的信息,但它不再有帮助了
this is firebaseconfig.js
`your// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from 'firebase/firestore';
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: ""
};
// Initialize Firebase
const firebaseApp = initializeApp(firebaseConfig);
// Initialize Firestore after initializing Firebase
const firestoreDB = getFirestore(firebaseApp);
// Export Firebase Auth and Firestore instances
export const firebaseAuth = getAuth(firebaseApp);
export { firestoreDB };`
this the register.js
\`import { createUserWithEmailAndPassword } from 'firebase/auth';
import { firebaseAuth } from '../database/firebaseconfig';
import { firestoreDB } from '../database/firebaseconfig';
const Register = () =\> {
const navigation = useNavigation();
const \[username, setUsername\] = useState('');
const \[email, setEmail\] = useState('');
const \[password, setPassword\] = useState('');
const \[confirmPassword, setConfirmPassword\] = useState('');
const handleRegister = () =\> {
if (!email || !password) {
Alert.alert('Error', 'Please fill in all fields');
return;
}
if (password !== confirmPassword) {
Alert.alert('Error', 'Passwords do not match');
return;
}
createUserWithEmailAndPassword(firebaseAuth, email, password)
.then((userCredential) => {
const user = userCredential.user;
Alert.alert('Success', 'Account created successfully!');
firestoreDB.collection('users').doc(user.uid).set({
username: username,
email: email,
})
.then(() => {
console.log('User information saved to Firestore');
})
.catch((error) => {
console.error('Error saving user information to Firestore: ', error);
});
})
.catch((error) => {
Alert.alert('Error', error.message);
});
};` 有人可以帮忙吗
帮助我解决这个问题,我想将这些信息保存在 firestore 中,但我不知道该怎么办
我认为您正在混合 Firebase JS SDK 模块化 API 和 Web 命名空间 API 实现。将数据写入Firestore数据库的正确方法如下:
import { doc, setDoc } from "firebase/firestore";
// Add a new document in collection "cities"
await setDoc(doc(db, "cities", "LA"), {
name: "Los Angeles",
state: "CA",
country: "USA"
});
请参阅此文档供您参考。