简体   繁体   English

访问子 firebase 数据库时的“app”属性是什么(javascript)

[英]What is `app` property when access child firebase database (javascript)

This is my file that initialize firebase:这是我初始化 firebase 的文件:

export default class {
  static parent = RNFirebase.initializeApp();
  static child = null;

  constructor() {
    child = this.parent.database('child-database-url');
  }
}

In another file I use this as:在另一个文件中,我将其用作:

import FDBS from '../initializer';

FDBS.child.app.database().ref('orders').once("value", data => {
  console.log(data);
});

And this line of code confuse me: FDBS.child.app.database().ref( actually .app .这行代码让我感到困惑: FDBS.child.app.database().ref(实际上是.app
Why I need to access this property to get data actually what I am trying to get is to be able to write code like: FDBS.child.database().ref('orders').once(...为什么我需要访问这个属性来获取数据,实际上我想要获取的是能够编写如下代码: FDBS.child.database().ref('orders').once(...

To start with, let's fix your syntax error regarding the "static" variables in your initializer.js file.首先,让我们修复有关initializer.js文件中“静态”变量的语法错误。 Instead of using a class for such a simple data structure, just export an object.对于这种简单的数据结构,不要使用 class,而只需导出 object。

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
}

Now, as shown in the above snippet, your parent object is an instance of the FirebaseApp object and the child object is an instance of the FirebaseDatabase object.现在,如上面的代码片段所示,您的parent object 是FirebaseApp object 的一个实例, child object 是FirebaseDatabase object 的一个实例。

In your code that loads in FDBS , you access FDBS.child.app ( reference ) which returns the FirebaseApp object associated with that instance of FirebaseDatabase - in your case this object is FDBS.parent .在加载到FDBS中的代码中,您访问FDBS.child.app参考),它返回与该FirebaseDatabase实例关联的FirebaseApp object - 在您的情况下,此 object 是FDBS.parent

As the two objects have different types, I recommend choosing to export either two FirebaseApp instances or two FirebaseDatabase instances.由于这两个对象具有不同的类型,我建议选择导出两个FirebaseApp实例或两个FirebaseDatabase实例。

Export FirebaseApp instances导出FirebaseApp实例

Based on your question, you seem to be expecting that the child object is also a FirebaseApp object because you want to call FDBS.child.database().ref(...) .根据您的问题,您似乎期望child object 也是FirebaseApp object 因为您想调用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
}

You would use this as follows:您将按如下方式使用它:

import FDBS from '../initializer';

FDBS.child.database().ref('orders').once("value", data => {
  console.log(data);
});

This approach introduces a problem when you involve authentication.当您涉及身份验证时,此方法会引入一个问题。 If a user signs into your app, they will be signed in for the default FirebaseApp instance but not the one used by FDBS.child (unless you explicitly do so).如果用户登录您的应用程序,他们将登录默认的 FirebaseApp 实例,而不是FDBS.child使用的实例(除非您明确这样做)。 Because of this I recommend the other approach.因此,我推荐另一种方法。

Export FirebaseDatabase instances (recommended)导出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
}

You would use this as follows:您将按如下方式使用它:

import FDBS from '../initializer';

FDBS.child.ref('orders').once("value", data => {
  console.log(data);
});

Note: Don't forget to handle errors.注意:不要忘记处理错误。 I encourage using promises rather than callbacks.我鼓励使用承诺而不是回调。

Edit in response to your comment编辑以回应您的评论

You don't show where RNFirebase comes from, but you should be able to access your database like你没有显示 RNFirebase 的来源,但你应该能够访问你的数据库

let initapp = RNFirebase.initializeApp()
let db = initapp.database('url')
db.ref....

Right now you've done that in a roundabout way现在你已经以迂回的方式做到了

child = this.parent.database( child = this.parent.database(

And if you check how you define parent, it's basically like the snippet I provided above.如果你检查一下你是如何定义父级的,它基本上就像我上面提供的片段。

So you could just be a little bit more concise less circular.所以你可以更简洁一点,少一些循环。

-- --

From the official docs, https://firebase.google.com/docs/reference/js/firebase.database.Database#app来自官方文档, https://firebase.google.com/docs/reference/js/firebase.database.Database#app

The app associated with the Database service instance.与数据库服务实例关联的应用程序。

Where app has on it the database property, among others https://firebase.google.com/docs/reference/js/firebase.app.App#database其中应用程序具有数据库属性,其中包括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);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM