问题
当我运行命令时:
npx expo run:android
npx expo start --reset-cache
我的 Android 手机已连接到我的 USB 端口。
我收到此错误:firebase.auth().useEmulator() 采用非空字符串 URL
我怀疑我的手机上运行的 Android 应用程序无法使用 USB 访问计算机 IP。
或者问题出在类型的定义中.d.ts,但我不知道如何解决方法的签名useEmulator()
无需模拟器命令,应用程序即可连接到 firebase。
控制台错误:
› Logs for your project will appear below. Press Ctrl+C to exit.
Android Bundled 754ms node_modules/expo-router/entry.js (1222 modules)
ERROR Error: firebase.auth().useEmulator() takes a non-empty string URL
This error is located at:
in TabTwoScreen
in Unknown (created by Route(explore))
in Suspense (created by Route(explore))
in Route (created by Route(explore))
in Route(explore) (created by SceneView)
in StaticContainer
in EnsureSingleNavigator (created by SceneView)
in SceneView (created by BottomTabView)
in RCTView (created by View)
in View (created by Screen)
in RCTView (created by View)
in View (created by Background)
in Background (created by Screen)
in Screen (created by BottomTabView)
in RNSScreen (created by Animated(Anonymous))
in Animated(Anonymous) (created by InnerScreen)
in Suspender (created by Freeze)
in Suspense (created by Freeze)
in Freeze (created by DelayedFreeze)
in DelayedFreeze (created by InnerScreen)
in InnerScreen (created by Screen)
in Screen (created by MaybeScreen)
in MaybeScreen (created by BottomTabView)
in RNSScreenContainer (created by ScreenContainer)
in ScreenContainer (created by MaybeScreenContainer)
in MaybeScreenContainer (created by BottomTabView)
in RCTView (created by View)
in View (created by SafeAreaProviderCompat)
in SafeAreaProviderCompat (created by BottomTabView)
in BottomTabView (created by BottomTabNavigator)
in PreventRemoveProvider (created by NavigationContent)
in NavigationContent
in Unknown (created by BottomTabNavigator)
in BottomTabNavigator
in Unknown (created by TabLayout)
in TabLayout
in Unknown (created by Route((tabs)))
in Suspense (created by Route((tabs)))
in Route (created by Route((tabs)))
in Route((tabs)) (created by SceneView)
in StaticContainer
in EnsureSingleNavigator (created by SceneView)
in SceneView (created by SceneView)
in RCTView (created by View)
in View (created by DebugContainer)
in DebugContainer (created by MaybeNestedStack)
in MaybeNestedStack (created by SceneView)
in RCTView (created by View)
in View (created by SceneView)
in RNSScreen (created by Animated(Anonymous))
in Animated(Anonymous) (created by InnerScreen)
in Suspender (created by Freeze)
in Suspense (created by Freeze)
in Freeze (created by DelayedFreeze)
in DelayedFreeze (created by InnerScreen)
in InnerScreen (created by Screen)
in Screen (created by SceneView)
in SceneView (created by NativeStackViewInner)
in Suspender (created by Freeze)
in Suspense (created by Freeze)
in Freeze (created by DelayedFreeze)
in DelayedFreeze (created by ScreenStack)
in RNSScreenStack (created by ScreenStack)
in ScreenStack (created by NativeStackViewInner)
in NativeStackViewInner (created by NativeStackView)
in RCTView (created by View)
in View (created by SafeAreaProviderCompat)
in SafeAreaProviderCompat (created by NativeStackView)
in NativeStackView (created by NativeStackNavigator)
in PreventRemoveProvider (created by NavigationContent)
in NavigationContent
in Unknown (created by NativeStackNavigator)
in NativeStackNavigator
in Unknown (created by RootLayout)
in ThemeProvider (created by RootLayout)
in RootLayout
in Unknown (created by Route())
in Suspense (created by Route())
in Route (created by Route())
in Route() (created by ContextNavigator)
in RNCSafeAreaProvider (created by SafeAreaProvider)
in SafeAreaProvider (created by wrapper)
in wrapper (created by ContextNavigator)
in EnsureSingleNavigator
in BaseNavigationContainer
in ThemeProvider
in NavigationContainerInner (created by ContextNavigator)
in ContextNavigator (created by ExpoRoot)
in ExpoRoot (created by App)
in App (created by ErrorOverlay)
in ErrorToastContainer (created by ErrorOverlay)
in ErrorOverlay (created by withDevTools(ErrorOverlay))
in withDevTools(ErrorOverlay)
in RCTView (created by View)
in View (created by AppContainer)
in RCTView (created by View)
in View (created by AppContainer)
in AppContainer
in main(RootComponent), js engine: hermes
ERROR Error: firebase.auth().useEmulator() takes a non-empty string URL
配置
npm 模块已启动:
npx expo install firebase
npx expo install @react-native-firebase/app
npx expo install @react-native-firebase/auth
FIREBASE 设置:
firebase init
模拟器设置:
firebase init emulators
我启动了模拟器:
firebase emulators:start
项目设置:
android/build.gradle
dependencies {
classpath('com.google.gms:google-services:4.4.2')
}
android/app/build.gradle
apply plugin: 'com.google.gms.google-services'
firebaseConfig.js
import { initializeApp } from 'firebase/app';
// Optionally import the services that you want to use
// import {...} from "firebase/auth";
// import {...} from "firebase/database";
// import {...} from "firebase/firestore";
// import {...} from "firebase/functions";
// import {...} from "firebase/storage";
// Initialize Firebase
const firebaseConfig = {
apiKey: '....',
authDomain: '.....firebaseapp.com',
projectId: '.....',
storageBucket: "......firebasestorage.app",
messagingSenderId: "......",
appId: "1:......:web:......"
};
const app = initializeApp(firebaseConfig);
根文件夹已配置firebase.json
我的android文件夹有:
google-services.json
调试.keystore
app.json
{
"expo": {
"plugins": [
"@react-native-firebase/app",
"@react-native-firebase/auth",
"expo-router"
],
}
}
探索.tsx
import auth from '@react-native-firebase/auth';
export default function TabTwoScreen() {
const fire = auth()
fire.useEmulator("192.168.1.26:9099")
}
我尝试使用:
fire.useEmulator("localhost:9099")
并尝试使用:
fire.useEmulator("10.0.2.2:9099")
此验证检查抛出的异常:
useEmulator(url) {
if (!url || !isString(url) || !isValidUrl(url)) {
throw new Error('firebase.auth().useEmulator() takes a non-empty string URL');
}
// ...
}
向此函数提供值 192.168.1.26:9099
通过
!url
和
!isString(url)
检查。但是如果我们仔细看看的实现,我们可以看到哪里出了问题:
/**
* URL test
* @param url
* @returns {boolean}
*/
const IS_VALID_URL_REGEX = /^(http|https):\/\/[^ "]+$/;
export function isValidUrl(url) {
return IS_VALID_URL_REGEX.test(url);
}
根据此正则表达式,我们可以看到提供的 URL 必须以 http://
或
https://
开头,后跟一些非空格或双引号字符的附加字符。这意味着您应该使用字符串
http://192.168.1.26:9099
作为 URL,假设
192.168.1.26
是托管模拟器的计算机的 IP 地址。