抱歉英语不好,因为我不会说英语,只会说谷歌翻译:)
我有一个问题..
我有这个 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/