Vue set Firebase data using Key

孤者浪人 提交于 2020-01-15 10:21:51

问题


I'm having trouble with a Vue Method that both pushes a new deal to one Firebase ref and sets data in another. The issues lies when the method sets data. My current method is unable to get the key of deal that it is writing to the dealsRef in order to save it in the dealsRel.

Firebase Refs:

// Firebase reference to deals
const dealsRef = db.ref('deals');
// Firebase reference to deal "relations"
const dealsRel = db.ref('dealsRel');

Method:

addDeal() {
  // Push new Deal to Firebase
  dealsRef.push(this.newDeal),
  //Issue is here:
  dealsRel.child(this.newDeal.provider).child( How to get key of object pushed in line above? ).set(true)
},

JSON Structure:

 

deals
    -KjtfMcfuZkP_kKP708x
        provider: "-KQ1if2Zv3R9FdkJz_1_"
        title: "Deal Name"
dealsrel
    -KQ1if2Zv3R9FdkJz_1_" // Provider Firebase ID
        Needs to be "-KjtfMcfuZkP_kKP708x": true //Firebase ID of deal
...

回答1:


Was able to solve using Firebase .getKey() like so:

var theKey = dealsRef.push(this.newDeal).getKey()    
dealsRel.child(this.newDeal.provider).child(theKey).set(true)


来源:https://stackoverflow.com/questions/43941623/vue-set-firebase-data-using-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!