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

280
Views
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 answers
Answer question

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 Report
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!