U
U
ura2rist2019-12-22 20:40:39
Node.js
ura2rist, 2019-12-22 20:40:39

How to push an array into an object by condition?

I write a condition, if == true then perform the following actions, if not, then just write the object, as a result, menuOtd is displayed in the console, but it is filled only in those places where else is triggered, i.e. the part after if is executed but not pushed into the general array. I output the val variable to the console, but everything is ok there, i.e. the code is executed but it does not push. What's the matter?

pnd.findOne({where: {id: 1}})
      .then(pnds => {
        pnds.getOtds()
        .then(otds =>{
          var menuOtd = [];
          for(ot of otds){
            var val = {};
            if(ot.pndOtd.sub == 'true'){
              otd.findOne({where: {id: ot.id}})
                .then(ots=>{
                  ots.getSubOtds()
                    .then(sub => {
                      var q =[];
                      for(a of sub){
                        //console.log('Отделения ' + ots.name + '!Под ' +a.name)
                        q.push(a.name);
                      }
                      val = {
                        name: ots.name,
                        sub: 'true',
                        pndId: 1,
                        subm: q
                      }
                      menuOtd.push(val);
                    })
                })
            }else{
              val = {
                name: ot.name,
                sub: 'false'
              }
              menuOtd.push(val);
            }
          }

          console.log(menuOtd)
          response.render('index',{
            pndli: pndli,
            pnd: info,
            table: table,
            otd: menuOtd
          });
        })
      });

Answer the question

In order to leave comments, you need to log in

3 answer(s)
H
hzzzzl, 2019-12-22
@hzzzzl

try this, if I understood the structure correctly

.then(sub => {
/*
                      var q =[];
                      for(a of sub){
                        //console.log('Отделения ' + ots.name + '!Под ' +a.name)
                        q.push(a.name);
                      }
*/
                      val = {
                        name: ots.name,
                        sub: 'true',
                        pndId: 1,
                        subm: sub.map(s => s.name)
                      }
                      menuOtd.push(val);
                    })

A
Andrey Suha, 2019-12-22
@andreysuha

You have asynchronous code inside the if, but not inside the else, so it turns out that the output to the console is triggered before the code inside the if is executed

I
Igor, 2019-12-23
@IgorPI

Colleague, your mistake is that inside the loop, the code is asynchronous.
The console output will happen sooner than you expect.
Here I have sloppily depicted the animation, but the essence is correct and clearly reflects your problem.
I use the cursor to show the order in which the code is executed.
Follow the cursor.
Outcome:
At the time of output to the console is menuOtdempty
Error type: logical

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question