I have a table name master_surveys, master_questions and master_answers. from those table I want to create a nested array result for json query, and I got some problem because the result is couldnt give any response
I have search what is the problem , but it seems like the problem is at this code and then i tried to change the code but the error become so many, so i decide to keep the first code
Here is my code
this.getSurvey = function(req,res,next) {
var id = req.query.id;
connection.acquire(function(err,con){
var survey = 'SELECT ms.id ,ms.title, ms.created_at, count(mq.id) question FROM master_surveys ms, master_questions mq WHERE ms.id=mq.survey_id';
con.query(survey, function(err,data){
if (data.length > 0) {
var survey = data[0];
var question = 'SELECT mq.id, mq.title, mq.type, mq.survey_id FROM master_surveys ms, master_questions mq WHERE ms.id=mq.survey_id and ms.id="'+survey.id+'"';
con.query(question, function(err, data){
if (data.length > 0) {
var answer = 'SELECT ma.id, ma.title, ma.selected_answer, ma.question_id FROM master_questions mq, master_answers ma WHERE mq.id=ma.question_id and mq.id="'+id+'"';
con.query(answer, function(err, data){
if(data.length>0){
question.data = data;
}
if (data.question== null) {
survey.data = [];
}
survey.data.push(question);
}), function (err) {
if (survey.questions.length == questions.length) {
res.json(survey);
}
}
;
};
});
};
});
});
};