在Firebase中使用push()时如何获取唯一的ID并存储在我的数据库中 [英] In Firebase when using push() How do I get the unique ID and store in my database
问题描述
我正在推送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
。这将生成一个新的推式ID,并将数据写入到具有该ID的位置。使用
push()$ c
- 。
$ 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 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 error is "ReferenceError: ref is not defined" You can get the key by using the function There are two ways to invoke using using Knowing #2, you can easily get a new push id client-side with: You can then use this key in your multi-location update. https://stackoverflow.com/a/36774761/2305342 If you invoke the Firebase You can then add the And write the item to the new location:
https://stackoverflow.com/a/34437786/2305342 in your case :
这篇关于在Firebase中使用push()时如何获取唯一的ID并存储在我的数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
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);
}
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);
}
key()
of any ref object
push
in Firebase's JavaScript SDK.
push(newObject)
. This will generate a new push id and write the data at the location with that id.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.var newKey = ref.push().key();
push()
method without arguments it is a
pure client-side operation.var newRef = ref.push(); // this does *not* call the server
key()
of the new ref to your item:var newItem = {
name: 'anauleau'
id: newRef.key()
};
newRef.set(newItem);
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);
}