javascript - 如何使用 Express JS 响应 JSON

标签 javascript mysql node.js express

抱歉英语不好,因为我不会说英语,只会说谷歌翻译:)

我有一个问题..

我有这个 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
                   }]
                }]
     }
]

最佳答案

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

关于javascript - 如何使用 Express JS 响应 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60764203/

相关文章:

javascript - 在循环中查找所有选项组合

php - 检查数据是否已经存在

javascript - Web RTC 屏幕共享在 Chrome 中不起作用

javascript - 当使用硬编码字符串作为参数和使用具有该值的变量时,方法返回不同的值

mysql - 基于多个条件和 JOINS 进行一对多求和

mysql - MySQL 重载时的性能问题

javascript - 使用 Node.js 监控 Mongo 的更改

javascript - Block.io NodeJS 模块没有 get_notifications 方法吗?

javascript - 上传多个文件时出现异步问题

javascript - Facebook 是否知道我正在使用 PhantomJS 抓取它,并且它可以更改其网站来对抗我吗?