[英]How to save the objects uid when I push the data in firebase database list, using javascript
I want to save the uid when I push new data in Firebase database. 当我在Firebase数据库中推送新数据时,我想保存uid。 This is possible not for auth users but for data objects.
对于身份验证用户,这不是可能的,但对于数据对象而言,这是可能的。
For example, I want this object schema: 例如,我想要这个对象模式:
"-Kdsfdsg555555fdsgfsdgfs" : { <------- I want this id
Id : "Kdsfdsg555555fdsgfsdgfs", <--- How to put that inside
name : "A",
time_updated : "6/6/2017"
}
Is any way how to get this id and pushed inside the object? 有什么方法可以获取此ID并将其推送到对象内部吗?
My code is as follows: 我的代码如下:
categories: FirebaseListObservable<any[]>;
this.categories = this.db.list('/categories') as FirebaseListObservable<Category[]>;
addCategory(category: any) {
return this.categories.push(category).then( snap => {
this.openSnackBar('New category has been added', 'ok');
}).catch(error => {
this.openSnackBar(error.message, 'ok');
});
}
There are two ways to use Firebase's push()
method: 有两种方法可以使用Firebase的
push()
方法:
You can use the second way of using push()
to just get the new location or its key: 您可以使用第二种方法使用
push()
来获取新位置或其键:
addCategory(category: any) {
var newRef = this.categories.push();
category.Id = newRef.key;
newRef.set(category).then( snap => {
this.openSnackBar('New category has been added', 'ok');
}).catch(error => {
this.openSnackBar(error.message, 'ok');
});
}
But note that it's typically an anti-pattern to store the key of an item inside that item itself too. 但请注意,通常也将项目的密钥存储在该项目本身内部也是一种反模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.