我一直在寻找解决这个错误的答案。奇怪的是,我的代码上周工作得非常好,但今天却不行。我想使用firebase auth为我的应用程序创建一个注册页面,但我一直得到无法读取属性'auth'的未定义错误。
firebase.js
import { initializeApp } from "firebase/app";
import { sendPasswordResetEmail, getAuth} from "firebase/auth";
import 'firebase/compat/firestore';
// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
//config code
};
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
export const sendPasswordReset = async (email) => {
try {
await sendPasswordResetEmail(auth, email);
alert("Password reset link sent!");
} catch (err) {
console.error(err);
alert(err.message);
}
};
export {app} ;
// // Initialize Realtime Database and get a reference to the service
// const database = getDatabase(app);
signupScreen.js
//SignupScreen.js
import {React, useState} from "react";
import { TouchableOpacity, TextInput, View, Text, Image } from "react-native";
import {styles} from "../styles/Styling";
import { acuitisLogo} from "../assets/images";
import { firebase } from "../components/firebase";
export default function SignupScreen({ navigation }) {
const [email, setEmail] = useState('');
const [password,setPassword] = useState('');
const [confirmPassword,setConfirmPassword] = useState('');
const [firstName,setFirstName] = useState('');
const [lastName,setLastName] = useState('');
const onRegisterPress = () => {
if (password !== confirmPassword) {
alert("Passwords don't match.")
return
}
firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then((response) => {
const uid = response.user.uid
const data = {
id: uid,
email,
firstName,
lastName,
};
const usersRef = firebase.firestore().collection('users')
usersRef
.doc(uid)
.set(data)
.then(() => {
navigation.navigate('Home', {user: data})
})
.catch((error) => {
alert(error)
});
})
.catch((error) => {
alert(error)
});
}
return (
<View style={ styles.stackpagecontainer }>
{/* <Image source = {acuitisLogo} style = {{alignSelf: "center", marginTop: 100}} /> */}
<Text style = {styles.title}> Sign up </Text>
<View style={styles.inputView}>
<TextInput
style = {styles.inputButtonText}
placeholder="First Name"
placeholderTextColor="#949494"
onChangeText={(firstName) => setFirstName(firstName)} />
</View>
<View style={styles.inputView}>
<TextInput
style = {styles.inputButtonText}
placeholder="Last Name"
placeholderTextColor="#949494"
onChangeText={(lastName) => setLastName(lastName)} />
</View>
<View style={styles.inputView}>
<TextInput
style = {styles.inputButtonText}
placeholder="Email"
placeholderTextColor="#949494"
onChangeText={(email) => setEmail(email)} />
</View>
<View style={styles.inputView}>
<TextInput
style = {styles.inputButtonText}
placeholder="Password"
placeholderTextColor="#949494"
secureTextEntry={true}
onChangeText={(password) => setPassword(password)} />
</View>
<View style={styles.inputView}>
<TextInput
style = {styles.inputButtonText}
placeholder="Confirm Password"
placeholderTextColor="#949494"
secureTextEntry={true}
onChangeText={(confirmPassword) => setConfirmPassword(confirmPassword)} />
</View>
<TouchableOpacity style = {styles.appButtonContainer} onPress={() => {onRegisterPress(); navigation.navigate("Login")}}>
<Text style = {styles.appButtonText}> Submit </Text>
</TouchableOpacity>
<TouchableOpacity style = {styles.existingButtonContainer} onPress={() => navigation.navigate("Login")}>
<Text style = {styles.loginButtonText}> ALREADY A MEMBER? - SIGN IN HERE </Text>
</TouchableOpacity>
</View>
);
}
似乎是“onRegisterPress”函数内的问题,请帮助
2条答案
按热度按时间tct7dpnv1#
我使用了这个链接:
https://blog.logrocket.com/user-authentication-firebase-react-apps/
作为灵感,所以我的代码再次运作,但仍然会感谢任何帮助,如果可能的话?
ljsrvy3e2#
export和import语句不匹配。
您正在firebase.js中导出
export {app} ;
但在另一个文件中,您导入的是
import { firebase } from "../components/firebase";