Retriving data from firebase web - javascript

I am trying to fetch the data for website using java script. Problem I am getting is that I am unable to get the key of Users-->history-->location-->from-->(lat,lng). I am unable to find a syntax of it.
Code I am trying is
var ref = firebase.database().ref("history/" + keys +"/location/from");
ref.on('value',gotDataa,errDataa);
function gotDataa(data){
//console.log(data.val());
var from = data.val();
var keys=Object.keys(from);
console.log(keys);
for(var i=0 ; i<keys.length;i++)
{
var k=keys[i];
var name = from[k].customer;
var phone = from[k].driver;
//console.log(name,phone,imag);
$('ul').append( '<li>' + name +" : " + phone+ '</li>' );
}
}

This should print the lat and lng of each item in the history:
var ref = firebase.database().ref("history/");
ref.on('value',gotDataa,errDataa);
function gotDataa(data){
data.forEach(function(child) {
var from = data.val().location.from;
console.log(from.lat+","+from.lng);
});
}

Related

How to render multiple html lists with AppScrip?

I am new to the world of * AppScript * I am currently designing a ** WepApp ** which is made up of html lists that connect to Mysql, when I individually test my lists paint correctly and their icons modify and update the data, without However ** the problem is ** when I join all my lists and call them through their corresponding url only the last one paints me the others are blank. For example of 10 lists I call # 2 the log tells me to call 10; I call 5 the same thing happens and if I call 10 it paints the data and they are allowed to be modified.
Within what I have searched I find that my problem lies in the way I render my pages but I cannot find the right path, so I ask for your support.
function doGet(e) {
var template ;
var view = e.parameters.v;
if(view == null){
template = HtmlService.createTemplateFromFile("Index");
}if(view == "Index"){
template = HtmlService.createTemplateFromFile("Index");
}if(view != null && view != "Index"){
template = HtmlService.createTemplateFromFile(view);
}
return template.evaluate()
.setTitle('Documental')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
function getTemplate(view){
return HtmlService.createTemplateFromFile(view);
}
and with this JavaScript method I connect my appscript code to pass it to my html
window.onload = function () {
google.script.run
.withSuccessHandler(run_This_On_Success)
.withFailureHandler(onFailure)
.readAreaPRE();
};
function onFailure(error) {
var div = document.getElementById("output");
div.innerHTML = "ERROR: " + error.message;
}
function run_This_On_Success (readAreaPRE) {
let table = $("#selectTable");
table.find("tbody tr").remove();
table.append("<tr><td>" + "</td><td>" + "</td></tr>");
readAreaPRE.forEach(function (e1, readAreaPRE) {
table.append(
"<tr><td>" +
e1[0] +
"</td><td>" +
e1[1] +
"</td><td>" +
"<p><a class='modal-trigger' id=" + e1[0] + " href='#modal1' onclick='capturaid("+e1[0]+",'"+ e1[1]+"')'><i class='material-icons'>edit</i></a>" +
"<a class='modal-trigger' href='#modal3' onclick='capturaidsup("+e1[0]+")'><i class='material-icons'>delete</i></a></p>" +
"</td></tr>"
);
});
};
function capturaidsup(dato1){
$("#delAreaPRE").val(dato1)
}
function capturaid(item1,item2) {
$("#uptAreaPRE1").val(item1);
$("#uptAreaPRE2").val(item2);
}
here is my function: readArePRE
function readAreaPRE() {
var conn = Jdbc.getCloudSqlConnection(url, user, contra);
var stmt = conn.createStatement();
stmt.setMaxRows(1000);
var results = stmt.executeQuery(
"CALL `BD_CENDOC_COL`.`sp_lee_tb_ref_AreasPRE`()"
);
var numCols = results.getMetaData().getColumnCount();
var rowString = new Array(results.length);
var i = 0;
while (results.next()) {
var id_areaPRE = results.getInt("id_areaPRE");
var AreaPRE = results.getString("AreaPRE");
rowString[i] = new Array(numCols);
rowString[i][0] = id_areaPRE;
rowString[i][1] = AreaPRE;
i++;
}
return rowString;
conn.close();
results.close();
}
Thank you in advance, any correction to ask my question will be welcome.

How to retreive data from Firebase with JavaScript

I am trying to retrieve some data from Firebase using JavaScript, but I have a problem. I can't get the items of my child "Usuarios".
This is how my database looks like
var rootRef = firebase.database().ref().child("Usuarios");
rootRef.on("child_added", snap => {
var name = snap.child("Nombre").val();
var email `enter code here`= snap.child("Apellido").val();
$("#table_body").append("<tr><td>" + name + "</td><td>" + email + "</td><td><button>Remove</button></td></tr>");
});
what #hazelcodes said, It would look something like this
var rootRef = firebase.database().ref().child("netflixfirebase-568e8/Usuarios");
I´ve got a solution, I added the function "childSnapshot"to the "Snapchot" that I already had.I checked each element (that is an Object) and took the properties they contained.
The code here:
var ref = firebase.database().ref ().child("Usuarios/");
ref.on("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var childData = childSnapshot.val();
var keystl = Object.keys(childData);
console.log(childData);
console.log(keystl);
for (var i = 0; i < keystl.length; i++)
{
var k = keystl[i];
var nombre = childData[k].Nombre;
var apellido = childData[k].Apellido;
console.log(nombre, apellido);
$("#table_body").append("<tr><td>" + nombre + "</td><td>" + apellido + "</td><td><button>Remove</button></td></tr>");
}
});
});

How to get the value of two nodes in Firebase?

I use firebase doc as reference to do the following code. I try other combinations with no success. There is no security access to database.
So, How can this code read or get value of two nodes at the same time?
function gofb(d) {
var r = d.getAttribute("data-id");
var p1name, p2name, pa1, pa2, pb1, pb2;
return firebase.database().ref('/wc2017/' + r + '/t1').once('value').then(
function(snapshot) {
pa1 = snapshot.val().pd1;
pa2 = snapshot.val().pd2;
p1name = pa1 + "|" + pa2;
});
return firebase.database().ref('/wc2017/' + r + '/t2').once('value').then(
function(snapshot) {
pb1 = snapshot.val().pd1;
pb2 = snapshot.val().pd2;
p2name = pb1 + "|" + pb2;
});
return firebase.database().ref('/wc2017/' + r).update({
a0_start: firebase.database.ServerValue.TIMESTAMP});
console.log(t1name);
console.log(t2name);
}
You can simply get the snapshot for the parent object's value. And get the values you want. Please refer to the following code.
firebase.database().ref('/wc2017/' + r).once('value',function(snapshot){
sValue = snapshot.val();
pa1t1 = sValue.t1.pd1;
pa2t1 = sValue.t1.pd2;
pb1t2 = sValue.t2.pd1;
pb2t2 = sValue.t2.pd2;
});

Making web form for updating data in Firebase

I am making an interface to manipulate data in Firebase. I managed to make a form that inserts new data, I have a list of retrieved data, the only thing that is missing is editing/updating existing data.
I am using Foundation 6 to speed up coding HTML and CSS and I created a modal that pops up when you click on a name. Also, I made that every name has a class that is their child key in Firebase so I can open data for the right item. I've stuck here and I don't know how to make that form in modal to fill data from clicked name. Can somebody help me a bit?
Here is my code:
var ref = new Firebase("https://myname.firebaseio.com/data/users");
ref.on("child_added", function(snapshot) {
var key = snapshot.key();
var data = snapshot.val();
var name = data.name;
var city = data.city;
$("#results").append($("<li class=\"" + key + "\">" + "<a data-open=\"modal\">" + name + ", " + city + "</a></li>"));
// this is for filling up the form with loaded data
$(".name").val(name);
$(".name").focus(function(){
$(this).val(name);
});
});
Try this:
var ref = new Firebase("https://myname.firebaseio.com/data/users");
var data;
ref.on('value', function(snapshot) {
data = snapshot.val();
$("#results").empty();
Object.keys(data).forEach(function(key) {
var value = data[key];
var name = value.name;
var city = value.city;
var li = $("<li class=\"" + key + "\">" + "<a data-open=\"modal\">" + name + ", " + city + "</a></li>");
$("#results").append(li);
li.click(function() {
$('#name').val(name);
$('#city').val(city);
$('#key').val(key);
});
});
});
$('form').submit(function() {
var key = $('#key').val();
var name = $('#name').val();
var city = $('#city').val();
data[key].name = name;
data[key].city = city;
ref.set(data);
return false;
});
JSFIDDLE

multi line variable in javascript

I am trying to make this javascript variable with some data from an array , but i cant figure out the right syntax to make this work..
certifications will be "Win7,Win8,PDI"
var myArray = certifications.split(",");
var data = "[{" +
for (var i in myArray)
" "id":i,"text":myArray[i]}, " +
"}]";
I'm hoping to get my data variable to look something like:
var data = "[{"id":0,"text":Win7},{"id":1,"text":Win8},{"id":2,"text":PDI}]";
Try this:
var data = JSON.stringify(certifications.split(",").map(function(value, index) {
return {
id: index,
text: value
};
}));
Maybe += is what your looking for:
var certifications = "Win7,Win8,PDI";
var myArray = certifications.split(",");
var data = "[{";
for (var i in myArray) {
data += " " +
"id" +":"+i+","+
"text" + ":"+myArray[i]+"}, ";
}
data += "}]";

Categories

Resources