简体   繁体   中英

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

This is my file that initialize 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 .
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(...

To start with, let's fix your syntax error regarding the "static" variables in your initializer.js file. Instead of using a class for such a simple data structure, just export an 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.

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 .

As the two objects have different types, I recommend choosing to export either two FirebaseApp instances or two FirebaseDatabase instances.

Export FirebaseApp instances

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(...) .

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). Because of this I recommend the other approach.

Export FirebaseDatabase instances (recommended)

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

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(

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

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

Gets the Database service for the current app. Example

var database = app.database();
// The above is shorthand for:
// var database = firebase.database(app);  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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