Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
Why is it showing that the function is not defined? It does log the results in console, I am just trying to list the first name

I have a restAPI backend that I am trying to get data from. I am using NodeJS but for some reason, the data loads in the console as JSON but when I try to print it in the new webpage I get an error

TypeError: /Final part 2/views/pages/letseat.ejs:11
    9|         </div>
    10|         <!-- Right here it takes every value that we got back and then puts it into a list item -->
 >> 11|             <% userlist.forEach(function(userlist) { %>
    12|                 <ul class="list-group list-group-numbered">
    13|                     <input type="checkbox" name=<%= userlist.firstname %>
    14|                     <li class="list-group-item"><%= userlist['firstname'] %>, By: <%= userlist['lastname' ]%> </li>

Cannot read properties of undefined (reading 'forEach')

But I do get the data in JSON in the terminal

  data: [
    {
      firstname: 'John',
      lastname: 'Boyle',
      numberofplaces: null,
      user_id: 1
    },
    {
      firstname: 'Ruben',
      lastname: 'Holt',
      numberofplaces: null,
      user_id: 3
    },
    {
      firstname: 'Tim',
      lastname: 'Diaz',
      numberofplaces: null,
      user_id: 4
    },
    {
      firstname: 'Tony',
      lastname: 'Santiago',
      numberofplaces: null,
      user_id: 2
    },
    {
      firstname: 'Scully',
      lastname: 'Smith',
      numberofplaces: null,
      user_id: 5
    }
  ]

The function that I am using to render the page is

    <% userlist.forEach(function(songlist) { %>
        <ul class="list-group list-group-numbered">
            <input type="checkbox" name=<%= userlist.firstname %>
            <li class="list-group-item"><%= userlist['firstname'] %>, By: <%= userlist['lastname' ]%> </li>
        </ul>
    <% }); %>  

Lastly this is the API route

app.get('/process_form', function(req, res){
    // create a variable to hold the username parsed from the request body
    // Now we will use the token wiht the name that the user entered
    // For this we will use the axios method to return the result
    axios.get('http://127.0.0.1:5000/api/getuser')
    .then((response)=>{
        var userlist = response.data.results;
        console.log(userlist);
        res.render('pages/letseat', {
            userlist: userlist
        });

Please give me some guidance I've been stuck on this for 3 hours!!!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are trying to access a property that doesn't exist and returning that caused it to be undefined.

    var userlist = response.data.results
//                               ^^^^^^^

response.data is already an array you wanted. So you gotta change to

    var userlist = response.data
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda