0

I am trying to loop through results that I have extracted from a database and write it to elements on a web page.

let customers = snapshot.numChildren();
console.log(customers);
let elements = document.getElementsByClassName('customerNumber');
            
customers.forEach(customer => {
    elements.textContent = customer;
});

I am seeing the results for customers in the console but I am also getting this error: Uncaught (in promise) TypeError: customers.forEach is not a function.

I am not sure what I am doing wrong here.

Ragnaboy
  • 23
  • 4
  • Note: elements is a nodelist - you can't `textContent` on a nodelist. You'd have to iterate over that too and apply `textContent` to each node. – Andy Aug 04 '21 at 09:42
  • This should help I suppose. it's very similar to your problem https://stackoverflow.com/questions/35969974/foreach-is-not-a-function-error-with-javascript-array – harel avv Aug 04 '21 at 09:42
  • What is `snapshot.numChildren`? Does it have a `forEach` method? It would appear that it isn't an array. – T.J. Crowder Aug 04 '21 at 09:50
  • 1
    Side note: `elements.textContent = customer` won't do anything. The `HTMLCollection` you get from `getElementsByClassName` doesn't have that property. See [here](http://stackoverflow.com/questions/10693845/what-do-queryselectorall-getelementsbyclassname-and-other-getelementsby-method) for details. – T.J. Crowder Aug 04 '21 at 09:51
  • Thanks for the responses. I am querying Firebase Database. All the responses made me realise that I have constructed the forEach loop incorrectly. Once I have sorted this out I will put my solution here. – Ragnaboy Aug 04 '21 at 10:26

0 Answers0