首页 > 解决方案 > React-Native-Firebase:RTDB 模拟器

问题描述

有人知道如何使用实时数据库模拟器吗?

我可以使用如下所示的功能和 Firestore 模拟器。

import functions from '@react-native-firebase/functions';
import firestore from '@react-native-firebase/firestore';

functions().useFunctionsEmulator('http://localhost:5001');
firestore().settings({ host: 'localhost:8080' });

但是无法为实时数据库找到类似的东西。任何链接/视频表示赞赏。

谢谢。

标签: javascriptfirebasefirebase-realtime-databasereact-native-firebasefirebase-tools

解决方案


基本上Frank van Puffelen发布的解决方案是正确的。但是,这让我陷入了一段时间,所以我将分享我的经验以节省其他人的时间。

import { firebase } from "@react-native-firebase/database";
const database = firebase
  .app()
  .database("http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE");
const ref = database.ref(`yourPath`)
...

巨大的陷阱来了:如果您使用的是 Android Emulator,则必须使用

const database = firebase
  .app()
  .database("http://10.0.2.2:9000?ns=YOUR_DATABASE_NAMESPACE");

如果您使用的是真正的 android 设备,则必须先设置反向代理

adb reverse tcp:9000 tcp:9000

然后正常设置数据库

const database = firebase
  .app()
  .database("http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE");

我没有测试 iOS,但我相信 localhost 应该可以工作。


推荐阅读