0

I have been trying to bringout json of junk pattern as below:

{"abc" : [ .... ] , "def":{"x":"3","y":4"}}

As per below code, I am able to get only abc object in response and not def in response. Could guess it is due to promise but ain't sure how to resolve. Please favour.

let finalData={};
db.zzz.findAll({where:condition}).then(output=>{
     finalData.abc=output; //output is array

      db.xxx.count({where:condition}).then(count=>{
         finalData.def.x=something;
          finalData.def.y=count;
          return finalData.def;
        });
      res.send(finalData);
});
Gayathri
  • 1,776
  • 5
  • 23
  • 50

1 Answers1

1

Follow along on the sequence journey:

STEP 1 -->    let finalData={};
STEP 2 -->    db.zzz.findAll({where:condition}).then(output=>{
STEP 3 -->         finalData.abc=output; //output is array

STEP 4 -->          db.xxx.count({where:condition}).then(count=>{
STEP 6 -->             finalData.def.x=something;
STEP 7 -->             finalData.def.y=count;
STEP 8 -->              return finalData.def;
                    });
STEP 5 -->          res.send(finalData);
              });

Do this:

let finalData={};
 finalData.abc=output; //output is array

  db.xxx.count({where:condition})
    .then(count=>{
       finalData.def.x=something;
       finalData.def.y=count;
       return;
    })
    .then(()=>res.send(finalData));

Follow the numbers...

Randy Casburn
  • 13,840
  • 1
  • 16
  • 31