Reputation: 63
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);
Upvotes: 0
Views: 1208
Reputation: 144
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);
Upvotes: 1