简体   繁体   English

当我使用javascript将数据推送到Firebase数据库列表中时如何保存对象uid

[英]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()方法:

  • By passing in an argument, it will generate a new location and store the argument there. 通过传递参数,它将生成一个新位置并将参数存储在该位置。
  • By passing in no arguments, it will just generate a new location. 通过不传入任何参数,它将仅生成一个新位置。

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.

相关问题 将数据推送到Firebase实时数据库时如何更改密钥 - How can I change key when I push data to firebase realtime database 有没有办法在没有 UID 的情况下将数据推送到 Firebase - Is there a way to push data into firebase without having the UID 在 Firebase 中使用 push() 如何获取唯一 ID 并存储在我的数据库中 - In Firebase when using push() How do I get the unique ID and store in my database 单击 javascript 按钮时,如何将数据推送到 firebase? - How do you push data to firebase when a javascript button is clicked? 如何使用云功能将新数据推送到Firebase实时数据库中? - How to push new data into firebase realtime database using cloud functions? 在 Firebase 中使用 push() 如何存储在我的数据库中 - In Firebase when using push() How to store in my database Javascript:将JSON数据保存到firebase数据库 - Javascript : Save JSON data to firebase database 如何使用JavaScript从Firebase实时数据库中检索对象数组? - How to retrieve an array of objects from a firebase realtime database using javascript? 使用jQuery(或Javascript)将对象列表推入数组 - Push A List of Objects Into an Array Using jQuery (Or Javascript) 如何使用 firebase 中的云函数向 uid 发送值? - How can I send a value to uid using cloud functions in firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM