[英]Get Key for object created after Push (Angular and Firebase)
I'm having problems understanding how to use Firebase. 我在了解如何使用Firebase时遇到问题。 I wrote a function to push some data in my firebase database.
我编写了一个函数,将一些数据推送到我的Firebase数据库中。 I need to get the Key generated after I push the data into my db.
我需要将数据推送到数据库后生成密钥。
This is my function 这是我的职责
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);
}
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. 当我将数据推送到事务处理路径时,它还会推送用户uid,因此我可以将数据从用户数据库链接到事务处理数据库。 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"
}
{
As you can see, I'm trying to console.log(order.$key) but it returns undefined. 如您所见,我正在尝试console.log(order。$ key),但它返回未定义。 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.