gpt4 book ai didi

firebase - 以编程方式更改默认的 firebase 数据库并在默认数据库和辅助数据库上运行相同的 firebase 函数

转载 作者:行者123 更新时间:2023-12-04 15:44:04 27 4
gpt4 key购买 nike

背景 -我正在设置一项新功能,允许用户选择他们所在的城市,因为我的应用程序是一个公共(public)交通应用程序。我希望城市位于单独的数据库中,为此我在我的 firebase 项目中创建了一个辅助数据库。这是一个 React Native 应用程序,我正在使用 react-native-firebase。

问题 1 -当用户选择不同的城市时,我希望该数据库成为他的默认数据库。我不知道该怎么做,有人可以帮忙吗?

我已经尝试初始化并仅更改数据库 URL,即使我连接到第二个数据库一次,也不是每次都这样做。似乎是一个不稳定的解决方案。

我发现的另一个解决方案是将 url 传递给每个“firebase.database(url)”,但这似乎是一个糟糕的解决方案。

问题 2 -由于这两个城市的应用程序完全相同,我想在 DB2 上运行我已经在 DB1 上运行的相同功能。它们是完全独立的数据库,但具有完全相同的节点。例如,两者都有一个“位置”节点,然后我有一个用于那里更改的监听器。如何仅使用一种功能在两个数据库上设置监听器?或者我是否需要获得对 DB2 的引用并只复制函数?

最佳答案

每个用户没有默认数据库的概念。这意味着您需要从 JavaScript 代码初始化 Firebase,如 React Native Firebase 中所示。文档:

// pluck values from your `GoogleService-Info.plist` you created on the firebase console
const iosConfig = {
clientId: 'x',
appId: 'x',
apiKey: 'x',
databaseURL: 'x',
storageBucket: 'x',
messagingSenderId: 'x',
projectId: 'x',

// enable persistence by adding the below flag
persistence: true,
};

// pluck values from your `google-services.json` file you created on the firebase console
const androidConfig = {
clientId: 'x',
appId: 'x',
apiKey: 'x',
databaseURL: 'x',
storageBucket: 'x',
messagingSenderId: 'x',
projectId: 'x',

// enable persistence by adding the below flag
persistence: true,
};

const kittensApp = firebase.initializeApp(
// use platform specific firebase config
Platform.OS === 'ios' ? iosConfig : androidConfig,
// name of this app
'kittens',
);

// dynamically created apps aren't available immediately due to the
// asynchronous nature of react native bridging, therefore you must
// wait for an `onReady` state before calling any modules/methods
// otherwise you will most likely run into `app not initialized` exceptions
kittensApp.onReady().then((app) => {
// --- ready ---
// use `app` arg, kittensApp var or `app('kittens')` to access modules
// and their methods. e.g:
firebase.app('kittens').auth().signInAnonymously().then((user) => {
console.log('kittensApp user ->', user.toJSON());
});
});

关于firebase - 以编程方式更改默认的 firebase 数据库并在默认数据库和辅助数据库上运行相同的 firebase 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56668853/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com