Problem calling javascript function

Asked

Viewed 159 times

-2

I am making a web application with javascript, my js file has several CRUD functions, I wanted when HTML was loaded, call two functions, so I added this code:

document.addEventListener('load', function() {
    ler() //le os dados do firebase
    mostrar() //mostra esse dados formatados
})

but it doesn’t work, what’s the problem?

Edit:

As a tip, I used the window in place of document and put a alert(), for testing and worked (the alert()), then I suppose something else is causing the problem

Here is the code of ler() and mostrar():

function ler() {
    json = []

    firebaseRef.once("value")
        .then(function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
            var key = childSnapshot.key
            var obj = childSnapshot.val()

            obj.key = key
            json.push(obj)

        })
    })
}

function mostrar() {
    receitas.innerHTML = ""

    for(var i = 0; i < json.length; i++) {
        receitas.innerHTML +=   '<div class="col s12 m4 l3">' +
                                    '<div class="card">' + 
                                        '<div class="card-image">' +
                                            '<img src="./image/default.png">' +
                                            '<span class="card-title">' + json[i].nome + '</span>' +
                                            '<span class="hide">' + json[i].key + '</span>' +
                                        '</div>' +
                                    '</div>' +
                                '</div>'
    }
}

Both work by calling the console functions

  • Show in your question the content of the function ler() and mostrar() along with the error

  • Where you define receitas?

  • var receitas = document.getElementById('receitas') is a div in HTML

  • Gives a console.log(json) to see if something is returning, at the end of ler

  • yes returns the objects correctly

2 answers

-1

Sometimes the event load may not be fired at document. Instead, use the window:

window.addEventListener('load', function() {
  alert('I am working!')
});
  • also doesn’t work

  • Try with the alert! If you receive the alert, the problem is in the functions ler and mostrar @Guillhermecostamilam

  • placed ler() and mostrar() in the question

  • Of course I have to close ) :)

  • Thanks!!! @wmsouza

  • Dude, I edited the code to involve the alert in a function expression because I think that was probably its intention in the first place, since previously the alert would be implemented in the evaluation of the arguments to be provided to the addEventListener (I mean, long before load even be issued).

Show 1 more comment

-1


I solved the problem by changing the function mudar(), instead of making a loop to show the data she received two parameters (key and name) and called the function by passing the values instead of adding in a json

Browser other questions tagged

You are not signed in. Login or sign up in order to post.