获取推送后创建的对象的密钥(Angular 和 Firebase)
Get Key for object created after Push (Angular and Firebase)
我在理解如何使用 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,因此我可以 link 将数据从我的用户数据库推送到事务数据库。我现在想要的是在我的数据库的其他部分 link 用户进行的交易,像这样:
transactionsPerUser {
user1 {
order.$key (key generated by push) : "true"
}
{
如您所见,我正在尝试 console.log(order.$key) 但它 returns 未定义。我该如何解决这个问题??其次,这是构建我的数据库的正确方法吗?感谢您的帮助!
我会对 post 行做一个小改动,然后将 / 添加到交易
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);
}
我在理解如何使用 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,因此我可以 link 将数据从我的用户数据库推送到事务数据库。我现在想要的是在我的数据库的其他部分 link 用户进行的交易,像这样:
transactionsPerUser {
user1 {
order.$key (key generated by push) : "true"
}
{
如您所见,我正在尝试 console.log(order.$key) 但它 returns 未定义。我该如何解决这个问题??其次,这是构建我的数据库的正确方法吗?感谢您的帮助!
我会对 post 行做一个小改动,然后将 / 添加到交易
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);
}