0

I am accepting data into a variable and I want the variable to be a part of a URL to point to a location in the Firebase db from where I want to retrieve the data.

Here is my code:

function Call()
{
    var dbref=new Firebase("https://dataretrievaltest-28983.firebaseio.com/EmployeeDB/EInfo").orderByKey();
    var login=localStorage.getItem("Email"); 
    console.log("Passed Email"+login);

    dbref
    .once("value")
    .then
        (
            function(snapshot)
            {
                snapshot.forEach
                (
                    function(childsnapshot)
                    {                      
                         var data = childsnapshot.val();
                         var emailval=data.Email;
                         if(login==emailval)
                         {
                         console.log(data.EID);
                         Eidval=data.EID;
                         }
                    }
                );
            }           
        );

    var dsRef=new Firebase("https://dataretrievaltest-28983.firebaseio.com/EmployeeDB/EDetail/EmployeeDB/EDetail/"+Eidval);
    dsRef
    .once("value")
    .then
        (
            function(snapshot)
            {
                //console.log(snapshot.val());
                snapshot.forEach
                (
                    function(childsnapshot)
                    {
                         var data = childsnapshot.val();
                         console.log(data);
                    }
                );
            }
        );
}

Above code is not working and appending /EDetail/"+Eidval is not producing any o/p
Eidval is defined as a global variable:

<body>
     <input type="button" value="Pull" id="btpull" onclick="Call()">
     <script src="https://cdn.firebase.com/js/client/2.4.2/firebase.js"></script>
     <script src="https://cdn.firebase.com/v0/firebase-simple-login.js"></script>

     <script src="https://www.gstatic.com/firebasejs/3.6.6/firebase.js"></script>
     <script>

     var Eidval=0;

     var config = {
                    apiKey: "AIzaSyA2LEx_3C0qPI75AexMPhCfZCxnVV-MVbU",
                    authDomain: "dataretrievaltest-28983.firebaseapp.com",
                    databaseURL: "https://dataretrievaltest-28983.firebaseio.com",
                    storageBucket: "dataretrievaltest-28983.appspot.com",
                    messagingSenderId: "572246196580"
                };
       firebase.initializeApp(config);
    </script>

Output:

Passed Emailchris@page.com
Phase1work.html:51 153
AL.
  • 36,815
  • 10
  • 142
  • 281
joel
  • 183
  • 1
  • 3
  • 15
  • Possible duplicate of [Global variables in Javascript across multiple files](http://stackoverflow.com/questions/2932782/global-variables-in-javascript-across-multiple-files) – GSerg Jan 20 '17 at 07:30

2 Answers2

0

Console log your Eidval and check if you have visibility of the variable at the statement level.

Jeyenth
  • 472
  • 4
  • 10
0

I shifted the code within function(childsnapshot) and it works!

joel
  • 183
  • 1
  • 3
  • 15