Vue set Firebase data using Key

2019-08-02 19:41发布

问题:

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)