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());
});
}
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 aREST API
.– relaxeaza
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.
– BrTkCa
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. =/
– michelmfreitas
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.– Rosário Pereira Fernandes