Javascript 使用javascript自动增加firebase中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39065786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto increment a value in firebase with javascript
提问by Kemal Ekren
Hello I'm working on some firebase project and i can save my datas via javascript into firebase database. But i couldn't figure it out to auto increment child value (my child value is duyuru, you can see the details in the below) of my database. I'm sharing my code in below can you give me hints to solve this issue.
你好,我正在做一些 firebase 项目,我可以通过 javascript 将我的数据保存到 firebase 数据库中。但是我无法弄清楚如何自动增加我的数据库的子值(我的子值是 duyuru,您可以在下面看到详细信息)。我在下面分享我的代码,你能给我解决这个问题的提示吗?
<script>
// Initialize Firebase
var config = {
apiKey: "sample1",
authDomain: "sample2",
databaseURL: "sample3",
storageBucket: "sample4",
};
firebase.initializeApp(config);
var database = firebase.database();
firebase.initializeApp(config);
var database = firebase.database();
function writeUserData(userId, name, email, imageUrl) {
var kategori1 = document.getElementById("kategori").value;
var duyuru1 = document.getElementById("duyuru").value;
var name1 = document.getElementById("name").value;
var email1 = document.getElementById("email").value;
var imageUrl1 = document.getElementById("imageUrl").value;
firebase.database().ref(kategori1 +"/" + duyuru1).set({
username: name1,
email: email1,
profile_picture : imageUrl1
});
}
</script>
and the out is like this
结果是这样的
{
"duyuru1": {
"email": "kjfdlkl",
"profile_picture": "dsfsd",
"username": "meraha"
}
}
I want to output data like;
我想输出这样的数据;
{
"duyuru1": {
"email": "kjfdlkl",
"profile_picture": "dsfsd",
"username": "meraha"
},
"duyuru2": {
"email": "kjfdlkl",
"profile_picture": "dsfsd",
"username": "meraha"
}
}
duyuru (child value) section should be auto increment, That's what i wanted for. Thank you for your answers
duyuru(子值)部分应该是自动递增的,这就是我想要的。谢谢您的回答
回答by Frank van Puffelen
Firebase has no auto-incrementing keys, since those don't work well in massively multi-user systems where clients can be offline for prolonged periods.
Firebase 没有自动递增的键,因为它们在客户端可能长时间离线的大规模多用户系统中不能很好地工作。
Instead, Firebase has its own type of auto-generated keys called push IDs. These push IDs have the same important properties of sequences in many relational databases: they are ordered and sequential. But in addition, they can be calculated client-side, even when the client is not connected to the Firebase servers.
相反,Firebase 有自己的自动生成密钥类型,称为推送 ID。这些推送 ID 与许多关系数据库中的序列具有相同的重要属性:它们是有序的和顺序的。但此外,它们可以在客户端计算,即使客户端未连接到 Firebase 服务器。
See the Firebase documentation on saving data in lists, this legacy blog post on why arrays don't work well in Firebaseand this post on how push ids work.
请参阅上表中的数据保存火力地堡文档,这个传统的博客文章为什么数组不火力地堡中很好地工作,这职位上推IDS如何工作。
回答by zavtra
Firebase recommends using distributed counters: https://firebase.google.com/docs/firestore/solutions/counters
Firebase 建议使用分布式计数器:https: //firebase.google.com/docs/firestore/solutions/counters
For the document that requires the counter, you'd initialize a subcollection of, say, 10 documents and run transaction increments on a random shard whenever you need a counter incremented.
对于需要计数器的文档,您可以初始化一个包含 10 个文档的子集合,并在需要计数器递增时在随机分片上运行事务递增。
Then you'd query the collection of shards and sum their counters.
然后您将查询分片的集合并将它们的计数器相加。
回答by Ozzy Ozmen Celik
There is no AutoId in firebase but here is a quick way to set as auto id
firebase 中没有 AutoId 但这里有一种设置为自动 id 的快速方法
Forexample ;
例如 ;
1- create yourModel ( which one u gonna send as model to firebase )
1- 创建你的模型(你将把哪个模型作为模型发送到 firebase )
2- DatabaseReference firebaseDatabase;
2- DatabaseReference firebaseDatabase;
3- firebaseDatabase =FirebaseDatabase.instance.reference();
3- firebaseDatabase =FirebaseDatabase.instance.reference();
4- firebaseDatabase.child("table_name").push().set( yourModel.toJson() );
4- firebaseDatabase.child("table_name").push().set( yourModel.toJson() );
Also for getting data u can write code like that
同样为了获取数据,您可以编写这样的代码
var result= firebaseDatabase.child("table_name").once().then(
(DataSnapshot datasnapshot){
Map<dynamic,dynamic> values= datasnapshot.value;
values.forEach((key,value){
print("key:"+key+" value:"+value["name"]);
});
}
);
print(result);
I tried it and works great
我试过了,效果很好
Have a nice day !!!
祝你今天过得愉快 !!!
回答by Hüseyin ?akanl?
You can use for first column key to "USERID + Your_indexNumber(in client var)" etc.
您可以将第一列键用于“USERID + Your_indexNumber(in client var)”等。
This is very simple method.
这是非常简单的方法。
Your table must like this:
你的桌子必须是这样的: