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

282
Vistas
pass object to mustache template

I'm working on a project using express with mustache templates, I'm trying to pass object to the template using res.locals but it's not working,

this is the code

exports.menu = function(req,res){
    res.locals.title = "Menu";
    res.locals.class_type ="main_nav";
    MenuDB.GetAvailableLunch()
        .then((list) => {
            res.locals.lunch = list;
            console.log('Promise Resolved');
        })
        .catch((err) =>{
            console.log('Promise rejected', err);
        })
    res.render('menu');
}

I want the list that comes from the function to be the lunch object which I have on the mustache page, any ideas how to do this? I know that I can just do it like this

.then((list) => {
            res.render('menu', {
                'lunch': list
            });

but I'm trying to do it this way because I will have another function to return another lists from the database, hope this is clear enough.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Build a promise-chain or array of independent promises and only call res.render() once everything is resolved.

For example...

exports.menu = async (req, res) => {
  const [lunch, another] = await Promise.all([
    MenuDB.GetAvailableLunch(),
    anotherFunction(),
  ]);

  res.render("menu", {
    title: "Menu",
    class_type: "main_nav",
    lunch,
    another
  });
};
over 4 years ago · Santiago Trujillo 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