什么是 `app` 属性 访问子 firebase 数据库 (javascript)
What is `app` property when access child firebase database (javascript)
这是我初始化 firebase 的文件:
export default class {
static parent = RNFirebase.initializeApp();
static child = null;
constructor() {
child = this.parent.database('child-database-url');
}
}
在另一个文件中,我将其用作:
import FDBS from '../initializer';
FDBS.child.app.database().ref('orders').once("value", data => {
console.log(data);
});
这行代码让我感到困惑:FDBS.child.app.database().ref(
实际上 .app
。
为什么我需要访问这个 属性 来获取数据实际上我想要获取的是能够编写如下代码:FDBS.child.database().ref('orders').once(...
根据您的评论进行编辑
你没有显示 RNFirebase 的来源,但你应该能够像
一样访问你的数据库
let initapp = RNFirebase.initializeApp()
let db = initapp.database('url')
db.ref....
现在你已经以迂回的方式做到了
child = this.parent.database(
如果你检查一下你是如何定义父级的,它基本上就像我上面提供的片段。
所以你可以更简洁一点,少一些循环。
--
来自官方文档,https://firebase.google.com/docs/reference/js/firebase.database.Database#app
The app associated with the Database service instance.
应用在其中包含数据库 属性,以及其他 https://firebase.google.com/docs/reference/js/firebase.app.App#database
Gets the Database service for the current app. Example
var database = app.database();
// The above is shorthand for:
// var database = firebase.database(app);
首先,让我们修复有关 initializer.js
文件中 "static" 变量的语法错误。对于这样一个简单的数据结构,不要使用 class,只需导出一个对象即可。
import RNFirebase from '@react-native-firebase/app';
import '@react-native-firebase/database';
// inits & returns the default FirebaseApp instance
const parent = RNFirebase.initializeApp(); // or RNFirebase.app() if initialized automatically
// gets a FirebaseDatabase instance that points to 'child-database-url'
const child = parent.database('child-database-url');
export default {
parent, // this is a FirebaseApp object
child // this is a FirebaseDatabase object
}
现在,如上面的代码片段所示,您的 parent
对象是 FirebaseApp
object and the child
object is an instance of the FirebaseDatabase
对象的一个实例。
在 FDBS
中加载的代码中,您访问 FDBS.child.app
(reference),其中 returns FirebaseApp
对象与 FirebaseDatabase
- 在你的例子中这个对象是 FDBS.parent
.
由于两个对象的类型不同,我建议选择导出两个 FirebaseApp
个实例或两个 FirebaseDatabase
个实例。
导出 FirebaseApp
个实例
根据你的问题,你似乎期望 child
对象也是一个 FirebaseApp
对象,因为你想调用 FDBS.child.database().ref(...)
.
import RNFirebase from '@react-native-firebase/app';
import '@react-native-firebase/database';
const parent = RNFirebase.initializeApp(/* config */); // or RNFirebase.app()
const child = RNFirebase.initializeApp({
...parent.options, // copies everything from the default app's configuration
databaseURL: 'child-database-url' // but overwrites the value of databaseURL
});
export default {
parent, // this is a FirebaseApp object that uses the default "parent" database
child // this is a FirebaseApp object that uses the "child" database
}
您可以按如下方式使用它:
import FDBS from '../initializer';
FDBS.child.database().ref('orders').once("value", data => {
console.log(data);
});
当您涉及身份验证时,此方法会引入一个问题。如果用户登录您的应用,他们将登录默认的 FirebaseApp 实例,而不是 FDBS.child
使用的实例(除非您明确这样做)。因此,我推荐另一种方法。
导出 FirebaseDatabase
个实例(推荐)
import RNFirebase from '@react-native-firebase/app';
import '@react-native-firebase/database';
const defaultApp = RNFirebase.initializeApp(/* config */); // or RNFirebase.app()
const dbParent = defaultApp.database();
const dbChild = defaultApp.database('child-database-url');
export default {
// app: defaultApp, // (if you want to export the FirebaseApp object too)
parent: dbParent, // this is a FirebaseDatabase object that points to the default "parent" database
child: dbChild // this is a FirebaseDatabase object that points to the "child" database
}
您可以按如下方式使用它:
import FDBS from '../initializer';
FDBS.child.ref('orders').once("value", data => {
console.log(data);
});
注意:不要忘记处理错误。我鼓励使用承诺而不是回调。
这是我初始化 firebase 的文件:
export default class {
static parent = RNFirebase.initializeApp();
static child = null;
constructor() {
child = this.parent.database('child-database-url');
}
}
在另一个文件中,我将其用作:
import FDBS from '../initializer';
FDBS.child.app.database().ref('orders').once("value", data => {
console.log(data);
});
这行代码让我感到困惑:FDBS.child.app.database().ref(
实际上 .app
。
为什么我需要访问这个 属性 来获取数据实际上我想要获取的是能够编写如下代码:FDBS.child.database().ref('orders').once(...
根据您的评论进行编辑
你没有显示 RNFirebase 的来源,但你应该能够像
一样访问你的数据库let initapp = RNFirebase.initializeApp()
let db = initapp.database('url')
db.ref....
现在你已经以迂回的方式做到了
child = this.parent.database(
如果你检查一下你是如何定义父级的,它基本上就像我上面提供的片段。
所以你可以更简洁一点,少一些循环。
--
来自官方文档,https://firebase.google.com/docs/reference/js/firebase.database.Database#app
The app associated with the Database service instance.
应用在其中包含数据库 属性,以及其他 https://firebase.google.com/docs/reference/js/firebase.app.App#database
Gets the Database service for the current app. Example
var database = app.database();
// The above is shorthand for:
// var database = firebase.database(app);
首先,让我们修复有关 initializer.js
文件中 "static" 变量的语法错误。对于这样一个简单的数据结构,不要使用 class,只需导出一个对象即可。
import RNFirebase from '@react-native-firebase/app';
import '@react-native-firebase/database';
// inits & returns the default FirebaseApp instance
const parent = RNFirebase.initializeApp(); // or RNFirebase.app() if initialized automatically
// gets a FirebaseDatabase instance that points to 'child-database-url'
const child = parent.database('child-database-url');
export default {
parent, // this is a FirebaseApp object
child // this is a FirebaseDatabase object
}
现在,如上面的代码片段所示,您的 parent
对象是 FirebaseApp
object and the child
object is an instance of the FirebaseDatabase
对象的一个实例。
在 FDBS
中加载的代码中,您访问 FDBS.child.app
(reference),其中 returns FirebaseApp
对象与 FirebaseDatabase
- 在你的例子中这个对象是 FDBS.parent
.
由于两个对象的类型不同,我建议选择导出两个 FirebaseApp
个实例或两个 FirebaseDatabase
个实例。
导出 FirebaseApp
个实例
根据你的问题,你似乎期望 child
对象也是一个 FirebaseApp
对象,因为你想调用 FDBS.child.database().ref(...)
.
import RNFirebase from '@react-native-firebase/app';
import '@react-native-firebase/database';
const parent = RNFirebase.initializeApp(/* config */); // or RNFirebase.app()
const child = RNFirebase.initializeApp({
...parent.options, // copies everything from the default app's configuration
databaseURL: 'child-database-url' // but overwrites the value of databaseURL
});
export default {
parent, // this is a FirebaseApp object that uses the default "parent" database
child // this is a FirebaseApp object that uses the "child" database
}
您可以按如下方式使用它:
import FDBS from '../initializer';
FDBS.child.database().ref('orders').once("value", data => {
console.log(data);
});
当您涉及身份验证时,此方法会引入一个问题。如果用户登录您的应用,他们将登录默认的 FirebaseApp 实例,而不是 FDBS.child
使用的实例(除非您明确这样做)。因此,我推荐另一种方法。
导出 FirebaseDatabase
个实例(推荐)
import RNFirebase from '@react-native-firebase/app';
import '@react-native-firebase/database';
const defaultApp = RNFirebase.initializeApp(/* config */); // or RNFirebase.app()
const dbParent = defaultApp.database();
const dbChild = defaultApp.database('child-database-url');
export default {
// app: defaultApp, // (if you want to export the FirebaseApp object too)
parent: dbParent, // this is a FirebaseDatabase object that points to the default "parent" database
child: dbChild // this is a FirebaseDatabase object that points to the "child" database
}
您可以按如下方式使用它:
import FDBS from '../initializer';
FDBS.child.ref('orders').once("value", data => {
console.log(data);
});
注意:不要忘记处理错误。我鼓励使用承诺而不是回调。