简体   繁体   中英

How to response JSON with Express JS

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
                   }]
                }]
     }
]

由于您从数据库中获得了正确的信息,因此只需从该对象中提取数据并以您想要的新格式添加它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM