Firebase ref () error when using variable as reference

0

Greetings. I am currently developing a page whose function is to recover data from Firebase. During the tests, when establishing the reference for obtaining the data, if I establish it in a fixed way it works without problem. But assigning a value dynamically through a variable, throws the following error in console:

-Uncaught TypeError: Can not read property 'length' of null at Function.each

This is the code:

firebase.initializeApp(config);
var encuentra = $(producto).val();
var database = firebase.database();
var referencia = database.ref(encuentra);
var reportes={};
referencia.once('value', function(datos)
{
    reportes=datos.val();
    $.each(reportes, function(indice,valor){//muestra datos}

Setting ref () as: database.ref ("product") does not present a problem, it works properly; but using the variable finds , which contains the value of a input: text seems to cause a null result or some inconsistency when accessing the data. Any ideas?

Beforehand, thank you.

    
asked by A. Rios 29.08.2017 в 18:36
source

1 answer

-1

Some data is missing to know exactly what it is, but try with the expression:

firebase.initializeApp(config);
var encuentra = $(producto).val();----> var encuentra = $("#producto").val()
var database = firebase.database();
var referencia = database.ref(encuentra);
var reportes={};
referencia.once('value', function(datos)
{
    reportes=datos.val();
    $.each(reportes, function(indice,valor){//muestra datos}

I imagine you have a input text type with id="producto" , so good refer to it with # followed by the id of the input.

    
answered by 02.09.2017 в 21:00