简体   繁体   English

是否可以在Firebase数据库引用路径上定义变量?

[英]Is it possible to define a variable on the firebase database references path?

I am trying to redefine all my apps database references so that I can write a specific set of of firebase rules (I need to add buildings and depts nodes inside the user node – read why: What is the best practice to write the rules of Firebase in a situation like this? ). 我正在尝试重新定义我所有的应用数据库引用,以便可以编写一组特定的firebase规则(我需要在用户节点内添加建筑物depts节点–请阅读原因: 编写Firebase规则的最佳实践是什么?在这种情况下? )。

I am using a firebase-config.js and exporting all my app's database references to the app's components so hopefully if I change this path I don't have to refactor the code on all the app's components. 我正在使用firebase-config.js并将我所有应用程序的数据库引用导出到该应用程序的组件,因此希望如果我更改此路径,则不必在所有应用程序的组件上重构代码。

I am trying to do it with a function (on my firebase-config.js file): 我正在尝试使用功能(在我的firebase-config.js文件上)执行此操作:

import * as firebase from "firebase";
import { mapState } from 'vuex';
import { store } from './store/store';


var config = {
    apiKey: "XXXXXXXX",
    authDomain: "XXXXXXXX.firebaseapp.com",
    databaseURL: "https://XXXXXXXX.firebaseio.com",
    projectId: "XXXXXXXX",
    storageBucket: "XXXXXXXX.appspot.com",
    messagingSenderId: "XXXXXXXX"
};
firebase.initializeApp(config)
export default !firebase.apps.length ? firebase.initializeApp(config) : firebase;

firebase.auth().onAuthStateChanged((user) => {
    if (user) {
       let userRef = firebase.database().ref('users').child(user.uid);
       let buildingsRef = userRef.child('buildings'); 
    }
})();
export const db = firebase.database(); 
export const usersRef = db.ref('users');
_// BUT THIS: "buildingsRef" is returning undefined!!! :(
export const buildingsRef = db.ref('users'+ "/" + buildingsRef) //buildingsRef is a variable
export const deptsRef = db.ref('depts');

The buldingsRef variable is returning undefined and it's writing the buildings to an undefined node. buldingsRef变量返回未定义,并将建筑物写入未定义的节点。

{
  "users" : {
    "6hwNde08Wuaa9bfReR28niSbOsF3" : {
      "name" : "João Alves Marrucho",
      "userEmail" : "joaoalvesmarrucho@hotmail.com"
    },
    "undefined" : {
      "-L9M4lM7oZFfmJKorxjC" : {
        "address" : "",
        "name" : "b1",
        "ownerID" : "6hwNde08Wuaa9bfReR28niSbOsF3"
      }
    }
  }
}

I writing to the node with the following method (vue.js): 我使用以下方法(vue.js)写入节点:

addBuilding: function () {
  let userId = firebase.auth().currentUser.uid;
  let buildingKey = buildingsRef.push().key
  this.newBuilding.ownerID = userId;
  buildingsRef.child(buildingKey).set(this.newBuilding);
}

Any thoughts on why the variable buildingsRef is returning undefined? 关于为什么变量buildingRef返回未定义的任何想法?

Here is my full firebase-config.js file: http://jsfiddle.net/UnXrw/85/ 这是我完整的firebase-config.js文件: http : //jsfiddle.net/UnXrw/85/

If I understand your question, I think you want something like this: 如果我理解您的问题,我想您需要这样的东西:

firebase.auth().onAuthStateChanged(user => {
  if (user) {
    let userRef = firebase.database().ref('users').child(user.uid);
    let buildingsRef = userRef.child('buildings');
    // now do whatever you want with those refs
  }
});

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

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