自动增加firebase上的数据 [英] Auto increment data on firebase
问题描述
资产:{
asset001 :{
name:DESK001,
owner:店主名称,
品牌:HP,
},
asset002 :{
name:NOTE001,
owner:owner's name,
brand:Dell,
},
$ / code $ / pre
但是,我不知道如何自动增加值在asset001...我已经看到了push()方法,但是我不知道如何检索数据。这是我到目前为止:
JavaScript
// Get来自HTML
的输入ID var inputName = document.getElementById(inputName);
var inputOwner = document.getElementById(inputOwner);
var inputBrand = document.getElementById(inputBrand);
var inputAsset = document.getElementById(inputAsset);
//设置FireBase的值
btnAsset.addEventListener('click',e => {
//获取输入数据
const asset = inputAsset.value ;
const owner = inputOwner.value;
const brand = inputBrand.value;
const name = inputName.value;
//数据库引用
var rootRef = firebase.database()。ref()。child(Assets);
// setar os valores
rootRef.child(asset).child(owner) .set(owner);
rootRef.child(asset).child(brand).set(brand);
rootRef.child(asset).child(name)。set(name );
解决方案您似乎经历了很多在创建密钥的时候遇到麻烦,一旦到达Asset999,就会遇到麻烦。
最好的方法是确保你没有不得不费力地创建一个独特的关键是让firebase为你做,然后一次性上传所有的信息。
var newKey = rootRef。 push().key;
$ b $ var objectToUpload = {
owner:owner,
brand:brand,
name:name
};
rootRef.child(newKey).set(objectToUpload);
I am currently trying to create something like this on Firebase:
"Assets": {
"asset001" : {
"name": "DESK001",
"owner: "owner's name",
"brand: "HP",
},
"asset002" : {
"name": "NOTE001",
"owner: "owner's name",
"brand: "Dell",
},
...
}
But, i dont know how to automatically increment the value on "asset001"... i have seen the push() method, but then i wouldn't know how to retrieve the data. Here's what i have so far:
JavaScript
//Get the inputs ID from HTML
var inputName = document.getElementById("inputName");
var inputOwner = document.getElementById("inputOwner");
var inputBrand = document.getElementById("inputBrand");
var inputAsset = document.getElementById("inputAsset");
//Set values on FireBase
btnAsset.addEventListener('click', e => {
//Get inputs data
const asset = inputAsset.value;
const owner = inputOwner.value;
const brand = inputBrand.value;
const name = inputName.value;
//database reference
var rootRef = firebase.database().ref().child("Assets");
//setar os valores
rootRef.child(asset).child("owner").set(owner);
rootRef.child(asset).child("brand").set(brand);
rootRef.child(asset).child("name").set(name);
解决方案 You seem to be going through a lot of trouble trying to make names for your assets. The way you are creating your keys you will run into trouble once you reach Asset999.
The best way to ensure that you do not have to bother with creating a unique key is letting firebase do it for you. Then upload all the info in one go.
var newKey = rootRef.push().key;
var objectToUpload = {
"owner": owner,
"brand": brand,
"name": name
};
rootRef.child(newKey).set(objectToUpload);
这篇关于自动增加firebase上的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!