How to access DOM element from within the Clientesdao.js model?

Asked

Viewed 120 times

0

I am making a query to the database and, to display the result in a #state element of the form, Nodejs does not recognize Document.getElementById('element'), nor jQuery.

Error:

ReferenceError: document is not defined

My method:

ClientesDAO.prototype.getEstados = function(res, res){
    var fb = this._firebase.database();
    var result = fb.ref().child('estados/').orderByChild('nome');
    result.once('value', function(snapshot){
        document.getElementById("estados").innerHTML(snapshot.val());
    });
}
  • 1

    Nodejs cannot access the DOM because it is server-side. You can render a .html and pass a property with the database data or create a REST API.

  • Exactly @michelmfreitas, Rafael is right. You will have to send the result somehow and receive on the front end. Are you using a template engine by any chance? It’s easier to help if you can detail how the page is doing and how this call is being made.

  • Exactly, you are correct, since nothing has been rendered yet. I am using EJS. However, even if I return the data with a snapshot.val() my controller receives an empty Undefined return. =/

  • You are getting an empty return because the Firebase reading happens asynchronously. This means that the moment you return the snapshot.val() it has not yet been initialized.

No answers

Browser other questions tagged

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