I want to create a forum.
Scenario:
When a user click on question list from forum, It will redirect a page where he can find the respective Question and list of answer,
Whenever i used below mentioned query, I found the question from the question table (im_forum_question
) on the new page.
'SELECT q.id, q.forum_question, q.forum_question_point
FROM im_forum_question as q '+
WHERE q.id='+"'"+req.params.id+"' "
But when i mix above code with answer fetching query below mentioned, I only get the already answered question. For an unanswered question it is showing an error.
'SELECT q.id, q.forum_question, q.forum_question_point, qa.forum_answer, qa.user_name_answer
FROM im_forum_question as q
INNER JOIN im_forum_question_answer as qa
ON qa.question_id = q.id
WHERE q.id='+"'"+req.params.id+"' "
Because if no one answered that question, it will not be stored in answer table (im_forum_question_answer
).
- Is there any way if number 2 query (Above mentioned) failed the number 1 query (Above mentioned) will execute. (if, else)
If no answer found form answer table only question should show.
exports.get_question_answer = function(req, res, next){ db.sequelize.query( 'SELECT q.id, q.forum_question, q.forum_question_point, qa.forum_answer, qa.user_name_answer FROM im_forum_question as q '+ ' INNER JOIN im_forum_question_answer as qa ON qa.question_id = q.id '+ ' WHERE q.id='+"'"+req.params.id+"' " ).then(function(data){ console.log('Logs for Data', data); var arr = data[0]; res.render('forum/question.ejs',{ success:'', error:'', session: req.session.user, data:arr }) }) }