Vue使用Key设置Firebase数据

Vue使用Key设置Firebase数据

本文介绍了Vue使用Key设置Firebase数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Vue方法时遇到了麻烦,这两个方法都会将新的交易推送到一个Firebase参考,并将数据设置为另一个。问题在于方法设置数据的时候。我当前的方法无法获得它正在写入至dealsRef的交易密钥,以便将其保存在dealsRel中。



Firebase Refs:

  // Firebase对交易的引用
const dealsRef = db.ref('deals');
// Firebase引用处理关系
const dealsRel = db.ref('dealsRel');

方法:

  addDeal(){
//将新交易推送到Firebase
dealsRef.push(this.newDeal),
//问题在这里:
dealsRel.child (this.newDeal.provider).child(如何获取上面推送的对象的键?).set(true)
},

JSON结构:


 交易
-KjtfMcfuZkP_kKP708x
提供商:-KQ1if2Zv3R9FdkJz_1_
标题:交易名称
dealsrel
-KQ1if2Zv3R9FdkJz_1_//提供商Firebase ID
需要成为-KjtfMcfuZkP_kKP708x:true // Firebase ID of deal
...


.getKey()就可以解决这个问题:

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


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
...
解决方案

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)

这篇关于Vue使用Key设置Firebase数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 13:58