Hi all,
I’m using a Firebase realtime database in my app. Using fetch I can GET and POST new items to the database.
The problem I have is trying to access or store the unique ID for each entry. I need that unique ID to append the database URL so I can edit a specific entry. Any advice or resources on how to do this would be much appreciated!
Here’s what I have at the moment:
Database Structure
{
"-KbRJK2tHQSfvoHD400o" :{
"description": "A description ",
"title":"A name"
},
"-KbRJQi7ntTTTGBSZSVX" :{
"description":"Another description",
"title":"Another name"
}
}
Getting the data
var Observable = require("FuseJS/Observable");
var Timer = require('FuseJS/Timer');
var activities = Observable();
var errorMessage = Observable();
var timer = Timer.create(function() {
load();
}, 100, true);
function load() {
fetch('https://[myDatabase].firebaseio.com/.json?auth=[mySecret]', {
method: 'GET',
cache: 'default',
headers: {
"Content-type": "application/json"
}
})
.then(function(result) {
if (result.status !== 200) {
console.log("Something went wrong :(");
return;
}
return result.json();
})
.then(function(data) {
var keys = Object.keys(data);
keys.forEach(function(key, index) {
if (index >= activities.length) {
var value = data[key];
activities.add(value);
}
})
});
};
module.exports = {
activities: activities,
errorMessage: errorMessage
};
Posting data
var Observable = require("FuseJS/Observable");
var title = Observable();
var description = Observable();
function submit() {
fetch('https://[myDatabase].firebaseio.com/.json?auth=[mySecret]', {
method: 'POST',
headers: {
"Content-type": "application/json"
},
body: JSON.stringify({
"title": title.value,
"description": description.value
}),
});
}
module.exports = {
title: title,
description: description,
submit: submit
}