0

In NodeJS, I want to create nested array, please check the below structure:

[
  {
    "id": 1,
    "name": "Afghanistan",
    "helps": [
      {
        "questions": "questions 1"
      }
    ]
  },
  {
    "id": 2,
    "name": "Angola",
    "helps": [
      {
        "questions": "Question 5?"
      }
    ]
  },
]

Here, I'm using ExpressJs and MySQL. My intention is to create an array as above through MySQL. Please see the following code:

See the model code:

commonSelectByCondition: function(params, errorcallback, successcallback) {
        var join = "";
        var order_by = "";
        if(typeof params.join != 'undefined' && params.join != ""){
            join = params.join;
        }
        if(typeof params.order_by != 'undefined' && params.order_by){
            order_by = params.order_by;
        }
        var query = "SELECT "+ params.select + " FROM " + params.table + join + "  WHERE " + params.condition + " " +order_by;
        // console.log(query);
        pool.query(query, function(error, results, fields) {
            if (error) {
                console.log(error);
                errorcallback(error);
            } else {
                successcallback(results);
            }
        });
},

Please see the controller code:

router.post('/country-help', function (req, res, next) {
    var select = "`id`,`name`";
    var table = "`countries`";
    var limit = limit;
    var offset = offset;
    var condition = " status = 'Active'";
    var params = {
        select: select,
        table: table,
        limit: limit,
        offset: offset,
        condition: condition
    }
    CommonModule.commonSelectByCondition(params, function(err,data){

    }, function (data) {
        if (data.length > 0) {
            var response = {
                resCode: STATUSCODE.OK,
                response: data
            }

            const copy = [];
            let helps = [];
            data.forEach(function(item){

                var jsonObj = {
                  "id":item.id,
                  "name":item.name,
                  "Helps":[]
                }; 

                //copy.push(jsonObj);
                //copy.push({"helps": helps});
                var select = "`question`,`gender`, `user_id`, `country`";
                var table = "`my_help`";
                var limit = limit;
                var offset = offset;
                var condition = "country = '"+item.name+"' AND is_active = '1'";
                var params_inner = {
                    select: select,
                    table: table,
                    limit: limit,
                    offset: offset,
                    condition: condition
                }
                CommonModule.commonSelectByCondition(params_inner, function(err,data){

                }, function (data) {
                    //console.log(data)

                    if(data.length > 0) {
                       for (var i = 0; i <= 1; i++) {
                            // console.log(data[0].gender);
                            var helps = {};
                            helps.question = data[0].question;
                            jsonObj.Helps.push(helps);
                        }
                    }
                });
                copy.push(jsonObj);
            });
            res.status(200).send(copy);
        }
    });
});

The result is below and I see the Helps contains nothing.

[
  {
    "id": 1,
    "name": "Afghanistan",
    "Helps": [

    ]
  },
  {
    "id": 2,
    "name": "Albania",
    "Helps": [

    ]
  }
]

0 Answers0