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

157
Vistas
Cannot access values of object in ejs view

The following ejs view outputs object for typeof().

  <% if(typeof msg !== 'undefined'){ %>
    <p><%- msg %> <%- typeof(user)  %> </p>   
  <% } %>

This will output the entire object in the view.

  <% if(typeof msg !== 'undefined'){ %>
    <p><%- msg %> <%- user  %> </p>   
  <% } %>

Like so,

{ _id: 6209571311be2a36444347fd, name: 'Joe', email: 'joe@e.com'}

However, when I try to access the values of user it results in either no output or an 'undefined' error.

For example, this code does not output the user.name value or throw any errors.

  <% if(typeof msg !== 'undefined'){ %>
     <p><%- msg %> <%- user.name  %></p>   
  <% } %>

While this code causes the 'Cannot read property 'name' of undefined'.

 <% if(typeof msg !== 'undefined'){ %>
   <p><%- msg %> <%- user[0]['name']  %></p>   
 <% } %>

Can anyone tell me how to fix this. Thanks!

about 4 years ago · Santiago Gelvez
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