I have a view page that uses the layout via <%- include(‘layouts/search_job’); %>. I want to import a variable into this layout without affecting the parent view. Here’s how I’m attempting it:
exports.lysearch = async (req, res) => {
try {
const listCities = function_new.listCities || [];
return res.render('layouts/search_job', { listCities });
} catch (error) {
console.error('error:', error);
return res.status(500).send('error 500');
}
};
In my layout (search_job.ejs), this is my code:
<select id="s_loc">
<% if (listCities && listCities.length > 0) { %>
<% listCities.forEach(function(item) { %>
<option value="<%= item.cit_id %>"><%= item.cit_name %></option>
<% }); %>
<% } %>
</select>
However, when I run the project, it renders an error: listCities is not defined at eval. How can I resolve this?
I’ve tried everything I can, but I only succeed when I import the variable into the parent view
Tú Ngô Ngọc is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.