unable to get the response in the res.json

16
May 15, 2019, at 6:00 PM

I am trying to call sql module inside the sql module and push the second level module into the first level module array so that i can get the result. But the i getting the null array in the output.

router.post('/mymsg', function(req, res, next) {
      var rootmsgid = req.body.rootmsgid;
      var request = new sql.Request()
        .input('rootmsgid', sql.Int, rootmsgid)
        .execute('sp_mymsg')
        .then(function(val) {
          var x = val.recordsets[0]
          var z = [];
          var y  = [];
          for (var i = 0; i < x.length; i++) {
            var t1 = parseInt(x[i].msg_id)
            var t2 = parseInt(x[i].sender_id)
            var t3 = x[i].timestamp
            var t4 = x[i].recipient
            var t5 = x[i].subject
            var t6 = x[i].body

         var request = new sql.Request()
        .input('msgid', sql.Int, t1)
        .execute('filenamex')
        .then(function( val) {
         y =  val.recordsets[0];
         z.push({
                  "msg_id": t1,
                  "sender_id": t2,
                  "timestamp": t3,
                  "recipient": t4,
                  "subject": t5,
                  "body": t6,
                  "filename": y
                })
    console.log(z)
        })

     }
      res.json(z)
        })
        .catch(function(err) {
          console.log(err);
          res.json({
            msg: "Invalid Input",
            success: false
          });
          res.end();
        });
    });

I am trying to call sql module inside the sql module and push the second level module into the first level module array so that i can get the result. But the i getting the null array in the output.

Answer 1

Welcome to async programming :)

Your res.json(z) gets executed before your for loop finishes. That's you are getting an empty array. Use the async module or use async/await to resolve.

READ ALSO
Converting Cypress file into a more appropriate format for exporting

Converting Cypress file into a more appropriate format for exporting

I am struggling to put best practises into action, converting an existing file for Cypress testing into a more appropriate format for exporting and importing

13
Gatsby build doesn&#39;t work when hosting on nginx

Gatsby build doesn't work when hosting on nginx

I'm trying to host my Gatsby application on a DigitalOcean sever using nginxI ran the build first using gatsby build and here's the result:

33
KMS encrypt file data using Boto3

KMS encrypt file data using Boto3

I am using the below code to KMS encrypt the data in a fileIs there a way to do something similar using boto3 in python?

45
How to Communicate between Activity and Service using LocalBroadcastManager in a different Process

How to Communicate between Activity and Service using LocalBroadcastManager in a different Process

I have a Service which is in a different process defined in Manifest and a MapboxMap Activity, and I just wanted to know how I can communicate between my Service and Activity using LocalBroadcastManager

32