简体   繁体   English

React Native - Expo 错误 - “在‘firebase’中找不到导出‘auth’:

[英]React Native - Expo Error - "export 'auth' was not found in 'firebase' :

Building an auth app using native with the expo.使用 expo 使用本机构建一个身份验证应用程序。

Create firebase.js file and link the app创建firebase.js文件并链接应用程序

exported auth properly in firebase.js still can't import auth in my LoginScreen.js filefirebase.js中正确导出auth仍然无法在我的LoginScreen.js文件中导入auth

It keeps throwing the error它不断抛出错误

"export 'auth' was not found in 'firebase'
  11 | 
  12 |     const handleSignup = () =>{
> 13 |         auth
     |        ^
  14 |         .createUserWithEmailAndPassword(email, 
password)
  15 |         .then(usersCredentials => {
  16 |             const user = userCredentials.user; 

Firebase.js Firebase.js

// Import the functions you need from the SDKs you need
// import * as firebase from "firebase";
import firebase from "firebase/app"
// import "firebase/auth"
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
const firebaseConfig = {
  apiKey: "AIzaSyB4R4he96dglIFlyGQyP8aYKLtzfGGc_ZQ",
  authDomain: "fir-auth-ec54a.firebaseapp.com",
  projectId: "fir-auth-ec54a",
  storageBucket: "fir-auth-ec54a.appspot.com",
  messagingSenderId: "211989329100",
  appId: "1:211989329100:web:765e2715889a0fc374be69"
};

// Initialize Firebase
 let app;
 if (firebase.apps.length == 0) {
     app = firebase.initializeApp(firebaseConfig)
 } else{
     app = firebase.app()
 }

 const auth = firebase.auth()

 export default { auth }

dependencies in package.json package.json 中的依赖项

"dependencies": {
    "@react-navigation/native": "^6.0.6",
    "@react-navigation/native-stack": "^6.2.5",
    "expo": "~44.0.0",
    "expo-status-bar": "~1.2.0",
    "firebase": "8.2.3",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-native": "0.64.3",
    "react-native-safe-area-context": "3.3.2",
    "react-native-screens": "~3.10.1",
    "react-native-web": "0.17.1"
  },

EDIT编辑

This is where I am importing the firebase:这是我导入 firebase 的地方:

import React, { useState } from 'react'
import { KeyboardAvoidingView, StyleSheet, Text, View } from 'react-native'
import { TextInput, TouchableOpacity } from 'react-native-web'
// import firebase from "firebase/app"
// import "firebase/auth"
import { auth } from '../firebase'

const LoginScreen = () => {
    const [email, setEmail] = useState('')
    const [password, setPassword] = useState('')
const handleSignup = () =>{
    auth
    .createUserWithEmailAndPassword(email, password)
    .then(userCredentials => {
        const user = userCredentials.user;
        console.log(user.email)
    })
    .catch(error => alert(error.message))
}

return (
    <KeyboardAvoidingView
        style={styles.container}
        behavior='padding'
    >
        <View style={styles.inputContainer}>
            <TextInput 
                placeholder="Email"
                value={email}
                onChangeText={text => setEmail(text) }
                style={styles.input}
            />
            <TextInput 
                placeholder="Password"
                value={password}
                onChangeText={text => setPassword(text) }
                style={styles.input}
                secureTextEntry
            />
        </View>

        <View style={styles.buttonContainer}>
            <TouchableOpacity
                onPress={() => {}}
                style={styles.buttonText}
            >`enter code here`
            <Text style={styles.button}>Log In</Text>
            </TouchableOpacity>
            <TouchableOpacity
                onPress={handleSignup}
                style={[styles.button, styles.buttonOutline]}
            >
            <Text style={styles.buttonOutlineText}>Register</Text>
            </TouchableOpacity>
        </View>
    </ KeyboardAvoidingView>

)}

export default LoginScreen

const styles = StyleSheet.create({
    container:{
        flex:1,
        justifyContent:'center',
        alignItems:'center',
    },
    inputContainer:{
        width: '80%',
    
},
input:{
    backgroundColor:'white',
    paddingHorizontal: 15,
    paddingVertical: 10,
    borderRadius: 10,
    marginTop: 5,
},
buttonContainer:{
    width: '60%',
    justifyContent:'center',
    alignItems:'center',
    marginTop: 40,
},
button:{
    backgroundColor: '#1674B5',
    width:'100%',
    padding:15,
    borderRadius:10,
    alignItems:'center'
},
buttonText:{
    color:'white',
    fontWeight:'700',
    fontSize:16,
},
buttonOutline:{
    backgroundColor:'white',
    marginTop:5,
    borderColor:'#1674B5',
    borderWidth:2,
},
buttonOutlineText:{
    color:'#1674B5',
    fontWeight:'700',
    fontSize:16,
},})

But now I can't see anything on the Web Browser Emulator.但是现在我在 Web 浏览器模拟器上什么也看不到。 VS code console is showing "Build Complete" . VS 代码控制台显示"Build Complete" What should I do to fix it?我应该怎么做才能解决它?

This is the Screenshot of my emulator now:这是我的模拟器现在的截图:

在此处输入图像描述

Snack is giving me this error零食给我这个错误

在此处输入图像描述

This line is exporting an object, as the default export, with a single property of auth :此行正在导出 object,作为默认导出,具有auth的单个属性:

export default { auth }

To import this object, you would use:要导入此 object,您将使用:

import lib from './yourfile.js';

const auth = lib.auth;
// or
const { auth } = lib;

However, if you intend on using:但是,如果您打算使用:

import { lib } from './yourfile.js';

Remove the default keyword to define an export called auth using either:删除default关键字以使用以下任一方式定义名为auth的导出:

const auth = firebase.auth()
export { auth }

or或者

export const auth = firebase.auth();

Firebase has recently undergone significant changes; Firebase最近发生了重大变化; make use of this.利用这个。

import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';

const firebaseConfig = {
  ....
};

// Use this to initialize the firebase App
const firebaseApp =firebase.initializeApp(firebaseConfig);

// Use these for auth

const auth = firebase.auth();

export {auth};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM