[英]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.