0

Sorry for bad English because I'm not speaking English, only Google Translate :)

I have one question..

I have this MySQL Query:

SELECT destinations.*, questions.*, answers.* FROM destinations INNER JOIN questions ON questions.quest_dest = destinations.dest_id INNER JOIN answers ON questions.quest_id = answers.answer_quest_id WHERE destinations.dest_active = 1

This Query working, not problem. I Have one modal file in Express JS.

const connection = require('../utility/databaseConnection');

module.exports = class Destinations{
    static AllDestinations(){
        return connection.execute("SELECT destinations.*, questions.*, answers.* FROM destinations INNER JOIN questions ON questions.quest_dest = destinations.dest_id INNER JOIN answers ON questions.quest_id = answers.answer_quest_id WHERE destinations.dest_active = 1");

    } }

If i get, not problem repsonse is come and... Not good come..

    [
        {
            "dest_id": 2,
            "dest_name": "İstanbul",
            "dest_lat": "41.0078254",
            "dest_lng": "28.9777912",
            "dest_active": "1",
            "quest_id": 1,
            "quest_text": "Bir dönemler Dünya'nın merkezi olmuş bu tarihi şehir neresidir?",
            "quest_dest": 2,
            "answer_id": 1,
            "answer_text": "Moskova",
            "answer_quest_id": 1,
            "answer_true": 0,
            "answer_point": 6400
        },
        {
            "dest_id": 2,
            "dest_name": "İstanbul",
            "dest_lat": "41.0078254",
            "dest_lng": "28.9777912",
            "dest_active": "1",
            "quest_id": 1,
            "quest_text": "Bir dönemler Dünya'nın merkezi olmuş bu tarihi şehir neresidir?",
            "quest_dest": 2,
            "answer_id": 2,
            "answer_text": "İstanbul",
            "answer_quest_id": 1,
            "answer_true": 1,
            "answer_point": 10000
        },
]

This is not good, i need This:

    [
        {
            "dest_id": 2,
            "dest_name": "İstanbul",
            "dest_lat": "41.0078254",
            "dest_lng": "28.9777912",
            "dest_active": "1",
            "questions": [
               {
                 "quest_id": 1,
                 "quest_text": "Bir dönemler Dünya'nın merkezi olmuş bu tarihi şehir neresidir?",
                 "quest_dest": 2,
                 "answers" : [
                   {  
                    "answer_id": 1,
                    "answer_text": "Moskova",
                    "answer_quest_id": 1,
                    "answer_true": 0,
                    "answer_point": 6400
                   },
                   {  
                    "answer_id": 2,
                    "answer_text": "İstanbul",
                    "answer_quest_id": 1,
                    "answer_true": 1,
                    "answer_point": 10000
                   }]
                }]
     }
]

1 Answer 1

1

Since you get the right information from the database, just extract the data from that object and add it in a new format that you desire.

Sign up to request clarification or add additional context in comments.

4 Comments

Is the problem in MySQL Query?
I would say your query is right, since you get correct data. However, you seem to have a format problem (you want your data to be stored in a specific order). To solve that, just create a new object, and add new properties to it in the order you want.
A query result will always be set of rows in other words you will not get the desired json structure by default just by using query result. So that as stated in @MajdJamal's answer you should have your own object and set it by manually.
In the old times I used PHP, I was able to make my own object using KEY EXIST. I couldn't understand how to do this in Express JS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.