Javascript React本机Firebase:RTDB仿真器
有人知道如何使用实时数据库模拟器吗 我可以使用函数和firestore模拟器,如下所示Javascript React本机Firebase:RTDB仿真器,javascript,firebase,firebase-realtime-database,react-native-firebase,firebase-tools,Javascript,Firebase,Firebase Realtime Database,React Native Firebase,Firebase Tools,有人知道如何使用实时数据库模拟器吗 我可以使用函数和firestore模拟器,如下所示 import functions from '@react-native-firebase/functions'; import firestore from '@react-native-firebase/firestore'; functions().useFunctionsEmulator('http://localhost:5001'); firestore().settings({ host: '
import functions from '@react-native-firebase/functions';
import firestore from '@react-native-firebase/firestore';
functions().useFunctionsEmulator('http://localhost:5001');
firestore().settings({ host: 'localhost:8080' });
但无法为实时数据库找到类似的内容。
欢迎提供任何链接/视频
谢谢。根据上的文档,这是通过以下方式完成的:
if (location.hostname === "localhost") {
var firebaseConfig = {
// Point to the RTDB emulator running on localhost.
// In almost all cases the ns (namespace) is your project ID.
databaseURL: "http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE"
}
var myApp = firebase.initializeApp(firebaseConfig);
var db = myApp.database();
}
我不确定在
react native firebase
中是否支持这一点,因为他们的文档中根本没有提到这一点。最符合逻辑的做法是:
由发布的解决方案基本上是正确的。然而,这让我陷入了一段时间,所以我将分享我的经验,为其他人节省时间
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设备,您必须首先进行设置:
然后正常设置数据库
const database = firebase
.app()
.database("http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE");
我没有测试iOS,但我相信localhost应该可以工作。我在我的根index.js文件中为我的react本机项目准备了以下内容:
// Use a local emulator in development
if (__DEV__) {
// If you are running on a physical device, replace http://localhost with the local ip of your PC. (http://192.168.x.x)
auth().useEmulator('http://localhost:9099');
functions().useFunctionsEmulator('http://localhost:5001');
database().useEmulator('localhost', 9000);
const db = firestore();
db.settings({ host: 'localhost:8080', ssl: false });
}
我的应用程序中没有其他内容需要修改。database().useEmulator
行完成了这个任务
我假设您已经首先使用firebase init仿真器
初始化了所有仿真器,并让它们与firebase仿真器一起运行:start
确保您在emulator中使用的是
${projectd}-默认rtdb
数据库。但是使用RNFirebase,我不需要做任何初始化应用程序
是的,看起来他们没有用于此的本机方法。你可以试试我刚才加的。谢谢,弗兰克。这对我来说是合理的,我会试试看。这样行吗?就API而言,调用似乎应该可以工作,但我很好奇它是否会调用模拟器。
const database = firebase
.app()
.database("http://localhost:9000?ns=YOUR_DATABASE_NAMESPACE");
// Use a local emulator in development
if (__DEV__) {
// If you are running on a physical device, replace http://localhost with the local ip of your PC. (http://192.168.x.x)
auth().useEmulator('http://localhost:9099');
functions().useFunctionsEmulator('http://localhost:5001');
database().useEmulator('localhost', 9000);
const db = firestore();
db.settings({ host: 'localhost:8080', ssl: false });
}