推送后创建的对象的获取密钥(Angular和Firebase) [英] Get Key for object created after Push (Angular and Firebase)

查看:65
本文介绍了推送后创建的对象的获取密钥(Angular和Firebase)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having problems understanding how to use Firebase. I wrote a function to push some data in my firebase database. I need to get the Key generated after I push the data into my db.

这是我的职责

  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,因此我可以将数据从我的用户数据库链接到事务处理数据库.我现在想要的是在数据库的其他部分链接用户进行的交易,如下所示:

When I push the data to transactions path, it also pushes user uid so I can link the data from my user database to transactions database. What I want now is to link in other part of my database the transactions made by users, something like this:

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

如您所见,我正在尝试console.log(order.$ key),但它返回未定义.我该如何解决?其次,这是构建数据库的正确方法吗?感谢您的帮助!

As you can see, I'm trying to console.log(order.$key) but it returns undefined. How can I solve this?? and second, is this a proper way to structure my database? Thanks for your help!

推荐答案

我将对邮寄行进行少量更改,并在交易中添加/

I would make a small change to the post line and add / to the transactions

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);
}

这篇关于推送后创建的对象的获取密钥(Angular和Firebase)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆