获取推送后创建的对象的密钥(Angular 和 Firebase) [英] Get Key for object created after Push (Angular and 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) 但它返回 undefined.我该如何解决这个问题??其次,这是构建我的数据库的正确方法吗?感谢您的帮助!
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屋!