在Firebase中使用push()时如何获取唯一的ID并存储在我的数据库中 [英] In Firebase when using push() How do I get the unique ID and store in my database

查看:492
本文介绍了在Firebase中使用push()时如何获取唯一的ID并存储在我的数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在推送Firebase中的数据,但是我想在我的数据库中存储唯一的ID。
可以有人告诉我,如何推送与唯一的id数据。

我想这样



write()$ {

var key = ref.push()。key();
var newData = {
id:key,
Website_Name:this.web_name.value,
用户名:this.username.value,
密码:this.password。值,
website_link:this.web_link.value
}

firebase.database()。ref()。push(newData);






$ b

错误是ReferenceError:ref is not定义

解决方案

您可以使用函数 key任何ref对象的c>


Firebase中有两种方法可以调用 push JavaScript SDK。使用 push(newObject)
$ b


  1. 。这将生成一个新的推式ID,并将数据写入到具有该ID的位置。使用 push()

  2. $ C>。这将生成一个新的推式ID,并返回一个引用到该ID的位置。这是纯粹的客户端
    操作


了解#2 ,你可以很容易地得到一个新的推客户端的客户端:

  var newKey = ref.push()。key() ; 

然后,您可以在多位置更新中使用此键。


https://stackoverflow.com/a/36774761/2305342


如果您调用不带参数的Firebase push()方法,
纯客户端操作。

  var newRef = ref.push();然后你可以添加,然后把 

  var newItem = {
name:'anauleau'
id:newRef.key()
};

然后将项目写入新位置:

  newRef.set(的newitem); 



$ / $> $ / $> $ / $> b

  writeUserData(){
var myRef = firebase.database()。ref()。push();
var key = myRef.key();

var newData = {
id:key,
Website_Name:this.web_name.value,
用户名:this.username.value,
密码: this.password.value,
website_link:this.web_link.value
}

myRef.push(newData);

}


I am pushing data in firebase, but i want to store unique id in my database also . can somebody tell me,how to push the data with unique id.

i am trying like this

  writeUserData() {

    var key= ref.push().key();
    var newData={
        id: key,
        Website_Name: this.web_name.value,
        Username: this.username.value,
        Password : this.password.value,
        website_link : this.web_link.value
    }

    firebase.database().ref().push(newData);


   }

error is "ReferenceError: ref is not defined"

解决方案

You can get the key by using the function key() of any ref object

There are two ways to invoke push in Firebase's JavaScript SDK.

  1. using push(newObject). This will generate a new push id and write the data at the location with that id.

  2. using push(). This will generate a new push id and return a reference to the location with that id. This is a pure client-side operation.

Knowing #2, you can easily get a new push id client-side with:

var newKey = ref.push().key();

You can then use this key in your multi-location update.

https://stackoverflow.com/a/36774761/2305342

If you invoke the Firebase push() method without arguments it is a pure client-side operation.

var newRef = ref.push(); // this does *not* call the server

You can then add the key() of the new ref to your item:

var newItem = {
    name: 'anauleau'
    id: newRef.key()
};

And write the item to the new location:

newRef.set(newItem);

https://stackoverflow.com/a/34437786/2305342

in your case :

writeUserData() {
  var myRef = firebase.database().ref().push();
  var key = myRef.key();

  var newData={
      id: key,
      Website_Name: this.web_name.value,
      Username: this.username.value,
      Password : this.password.value,
      website_link : this.web_link.value
   }

   myRef.push(newData);

}

这篇关于在Firebase中使用push()时如何获取唯一的ID并存储在我的数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆