1

Im stuck with the following problem:

I am fetching a json object from my nodejs backend that is a result of the following MYSQL operation

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
            })
        }
    });
}); 

On the client side (which is a react app) im using the following fetch:

fetch(`${global.x}/get_events/${playgroundId}`)
    .then((res) => res.json())
    .then((res) => {
        const data = res.data  
        return data
    
    }).catch((error) => {
      //console.log(error)
    });

the whole thing basically return a grouped by date json response about events that i got in the database. The final data looks like this:

Array [ Object { "date": "2021-03-17T14:52:18.000Z", "list": "[{"id": "cd66d921-8688-11eb-909e-06a351dd0cca", "mode": "Public", "time": "2021-03-16 19:52:18.000000", "type": "Game", "invited": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "members": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "waiting": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "admin_id": "ucLYuwevW5f2uTjdCZpVLBay6kq1", "event_name": "Ttt", "description": "Vvv", "playground_id": "a32bb687-7ae3-11eb-909e-06a351dd0cca"},{"id": "e2ecc0e4-868a-11eb-909e-06a351dd0cca", "mode": "Public", "time": "2021-03-16 21:07:06.000000", "type": "Game", "invited": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "members": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "waiting": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "admin_id": "ucLYuwevW5f2uTjdCZpVLBay6kq1", "event_name": "Tt", "description": "Ggg", "playground_id": "a32bb687-7ae3-11eb-909e-06a351dd0cca"},{"id": "eed0ee4c-8688-11eb-909e-06a351dd0cca", "mode": "Public", "time": "2021-03-16 19:52:18.000000", "type": "Game", "invited": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "members": ["ucLYuwevW5f2uTjdCZpVLBay6kq1"], "waiting": ["ucLYuwe]", }, ]

If i just have 2 events in the DB, the string is properly closed and there is no error. But once I have more than 2 events in there, it seems that the string becomes too long or something and just ends abruptly without proper escapes.

I am not sure whether there is a limit on characters coming from the DB or whether is a json parsing part on the client side.

The error I get in the terminal: SyntaxError: JSON Parse error: Unterminated string

If anybody had similar issues, could you please help.

Thank you!

2
  • 1
    As a first step I would test your query directly on the DB and see if you get a full JSON object there. That will tell you if the DB is causing any truncation or anything. Then step through the code one line at a time to see if it is returned as a full string. If it is, then some other step is causing the truncation. Commented Mar 16, 2021 at 19:51
  • 1
    I think i mostly figured it out. It is probably a group_concat_max_len parameter. It is defaulted to 1024. I will play with that and will post an answer later today Commented Mar 16, 2021 at 20:16

1 Answer 1

1

Changing group_concat maximum length like so: SET GLOBAL group_concat_max_len = 100000; fixed it. Had to move the DB inhouse cuz aws rds wouldnt allow changing the default length.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.