我目前正在尝试在Firebase上创建类似的内容:
"Assets": {
"asset001" : {
"name": "DESK001",
"owner: "owner's name",
"brand: "HP",
},
"asset002" : {
"name": "NOTE001",
"owner: "owner's name",
"brand: "Dell",
},
...
}
但是,我不知道如何自动增加“asset001”的值...我已经看到了push()方法,但后来我不知道如何检索数据。这是我到目前为止所拥有的:
的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);
答案 0 :(得分:1)
您似乎在尝试为您的资产命名时遇到了很多麻烦。一旦你达到Asset999,你创建密钥的方式就会遇到麻烦。
确保您不必费心创建唯一密钥的最佳方法是让firebase为您完成。然后一次上传所有信息。
var newKey = rootRef.push().key;
var objectToUpload = {
"owner": owner,
"brand": brand,
"name": name
};
rootRef.child(newKey).set(objectToUpload);