簡體   English   中英

如何使用 Express JS 響應 JSON

[英]How to response JSON with Express JS

抱歉英語不好,因為我不會說英語,只會谷歌翻譯 :)

我有一個問題..

我有這個 MySQL 查詢:

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

此查詢有效,沒有問題。 我在 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");

    } }

如果我得到,不是問題回應來了......不好來......

    [
        {
            "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
        },
]

這不好,我需要這個:

    [
        {
            "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
                   }]
                }]
     }
]

由於您從數據庫中獲得了正確的信息,因此只需從該對象中提取數據並以您想要的新格式添加它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM