【发布时间】:2021-06-14 03:43:50
【问题描述】:
我遇到了以下问题:
我正在从我的 nodejs 后端获取一个 json 对象,这是以下 MYSQL 操作的结果
app.get('/get_events/:playgroundId', cors(), function(req,res){
var playgroundId = req.params.playgroundId;
var sql = "SELECT date as date, CONCAT('[',GROUP_CONCAT(JSON_OBJECT('id', event_id,'time', time,'admin_id', admin_id,'playground_id', playground_id,'event_name', event_name,'description', description,'mode', mode,'type', type,'members', members,'waiting', waiting,'invited', invited)),']') AS list FROM xxx.events where playground_id = '"+playgroundId+"' GROUP BY DATE(date) ORDER BY DATE(date)";
pool.query(sql, function(err, results) {
if(err) {
console.log(err)
return res.send(err)
} else {
return res.json({
data: results
})
}
});
});
在客户端(这是一个反应应用程序)我使用以下提取:
fetch(`${global.x}/get_events/${playgroundId}`)
.then((res) => res.json())
.then((res) => {
const data = res.data
return data
}).catch((error) => {
//console.log(error)
});
整个事情基本上返回一个按日期分组的关于我在数据库中得到的事件的 json 响应。最终数据如下所示:
数组 [ 目的 { “日期”:“2021-03-17T14:52:18.000Z”, “列表”:“[{”id”:“cd66d921-8688-11eb-909e-06a351dd0cca”,“模式”:“公共”,“时间”:“2021-03-16 19:52:18.000000”,“类型": "游戏", "邀请": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "会员": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "等待中": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "admin_id": "ucLYuwevW5f2uTjd", "Cevent_pVLBW5f2uTjd" ”,“描述”:“Vvv”,“playground_id”:“a32bb687-7ae3-11eb-909e-06a351dd0cca”},{“id”:“e2ecc0e4-868a-11eb-909e-06a351dd0cca”,“模式”:“公共", "时间": "2021-03-16 21:07:06.000000", "类型": "游戏", "邀请": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "成员": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "等待中" :[“ucLYuwevW5f2uTjdCZpVLBay6kq1”],“admin_id”:“ucLYuwevW5f2uTjdCZpVLBay6kq1”,“event_name”:“Tt”,“描述”:“Ggg”,“playground_id”:“a32bb687-7ae3-11eb-909e-06a351dd0cca”},{ id”:“eed0ee4c-8688-11eb-909e-06a351dd0cca”,“模式”:“公共”,“时间”:“2021-03-16 19:52:18.000000”,“类型”:“游戏”,“邀请": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "成员": ["ucLYuwevW5f2uTjdCZ pVLBay6kq1"], "等待": ["ucLYuwe]", }, ]
如果我在数据库中只有 2 个事件,则字符串已正确关闭并且没有错误。但是一旦我在那里有超过 2 个事件,似乎字符串变得太长或什么的,并且在没有适当的转义的情况下突然结束。
我不确定来自数据库的字符是否有限制,或者客户端是否有 json 解析部分。
我在终端中得到的错误:SyntaxError: JSON Parse error: Unterminated string
如果有人遇到类似问题,请帮忙。
谢谢!
【问题讨论】:
-
作为第一步,我将直接在数据库上测试您的查询,看看您是否在那里获得了完整的 JSON 对象。这将告诉您数据库是否导致任何截断或任何事情。然后逐行单步执行代码,查看它是否作为完整字符串返回。如果是,则其他步骤导致截断。
-
我想我主要是想通了。它可能是一个 group_concat_max_len 参数。它默认为 1024。我将使用它,并将在今天晚些时候发布答案
标签: mysql sql json reactjs react-native