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

104
Views
variable not changing outside the promise

Im trying to push the results into a variable, the variable changes when I use the push but at the end of the function when I console the variable it shows the value initialized at the beginning of the function. It would be great if someone could solve this problem.

const getProductFilters = async (req, res) => {
      var result = [];
      await Product.findById(req.params.id)
        .then(async (val) => {
          await val.filterIds.forEach(
            async (item) =>
              await Filter.findById(item)
                .then(async (itm) => {
                  await itm.variableIds.forEach(async (val) => {
                    await Variable.findById(val)
                      .then((a) => {
                        console.log("1--", result);
                        !result.includes(a.name) ? result.push(a.name) : "";
                      })
                      .catch((err) => console.log(err));
                  });
                })
                .catch((err) => console.log(err))
          );
          console.log("2--", result);
        })
        .catch((err) => console.log(err));
    };

I'm getting this result:

2-- []
1-- []
1-- [ 'size' ]
1-- [ 'size', 'color' ]
1-- [ 'size', 'color' ]
about 4 years ago · Juan Pablo Isaza
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!