Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!