首页 文章

获取Push(Angular和Firebase)之后创建的对象的密钥

提问于
浏览
1

我在理解如何使用Firebase时遇到了问题 . 我写了一个函数来推送我的firebase数据库中的一些数据 . 我需要在将数据推送到数据库后生成密钥 .

这是我的功能

postData(order: orderInfo) {
    let uid = firebase.auth().currentUser.uid;
    this.db.list('transactions').push({
      currency: order.currency,
      total: order.total,
      rate: order.rate,
      uid: uid
    });
    console.log(order.$key);
    this.db.list('transactionsPerUser').update(uid, order.$key);
  }

当我将数据推送到事务路径时,它还会推送用户uid,以便将数据从我的用户数据库链接到事务数据库 . 我现在想要的是在我的数据库的其他部分链接用户进行的交易,如下所示:

transactionsPerUser {
     user1 {
           order.$key (key generated by push) : "true"
                     }
            {

正如您所看到的,我正在尝试console.log(order . $ key)但它返回undefined . 我怎么解决这个?第二,这是构建我的数据库的正确方法吗?谢谢你的帮助!

1 回答

  • 3

    我会对帖子行做一个小改动并添加/到交易

    postData(order: orderInfo) {
        let uid = firebase.auth().currentUser.uid;
    
    // Change this next line to save the transaction
    
        var newRef = this.db.list('/transactions').push({
          currency: order.currency,
          total: order.total,
          rate: order.rate,
          uid: uid
        });
    
    // get just the key reference
    var newKey= newRef.key;
    
    //then change this refer
    
        console.log(newKey);
    
    //To update you would change the next line
    
        this.db.list('transactionsPerUser').update(newKey, order.newInfo);
    }
    

相关问题