1

There exists a database with a users table containing 2 columns, userId and saved. userId is a simple increment, and saved is JSON data.

There exists a single row:

userId: 1, saved: "{"value": 1}"

When the following code is run on Windows

    const mysql = require('mysql2');
    
    async function start() {
        const connection = mysql.createConnection({
            host: 'localhost',
            user: 'root',
            database: 'database'
        });
        connection.query('SELECT saved FROM users WHERE userId = 1', function(err, results, fields) {
            console.log(results);
        })
    }
    
    start();

[ TextRow { saved: '{"value": 1}' } ] is displayed on Windows

[ TextRow { saved: { value: 1 } } ] is displayed on Linux

What exactly is going on here that parses my JSON on Linux but not on Windows?


The MySQL versions are both different

mysql Ver 14.14 Distrib 5.7.33, for Linux (x86_64)

mysql Ver 15.1 Distrib 10.4.17-MariaDB, for Win64 (AMD64)

I'm unsure how I'd get both of them to the same version. The Windows machine is running XAMPP and it seems like a pain to mess with. I'm trying right now to get the same versions of both installed.

The users table is InnoDB utf8mb4_general_ci

field type
userId int(10)
saved longtext utf8mb4_bin

I'm not actually using this code for anything, I use an ORM to do this but it uses mysql2 to do querying and I traced the error down to mysql2 and just copy-pasted the example code from GitHub to test if it was the culprit. I would use promise-based code if I was actually working with it.

MySQL2 is indeed the same version on both platforms. 2.2.5.

Interestingly the issue doesn't occur with the mysql package. I assume this indicates something funky going on with the package itself.

bluewave41
  • 135
  • 2
  • 13
  • 2
    Are both MySql Servers the very same version? – Alejandro Feb 10 '21 at 19:44
  • 2
    What's the DB schema in both cases? – Alejandro Feb 10 '21 at 20:08
  • Sorry for the off-topic question. I'm curious why people use callbacks when `mysql2/promise` is also available. Is it a preference or just an older habit? – Evert Feb 10 '21 at 20:17
  • I would also throw in, is the mysql2 package version identical for both? I think automatically decoding JSON fields is not a feature that was in mysql2 from the start. – Evert Feb 10 '21 at 20:18
  • I added more to the bottom of the post to address everything said so far as there's a lot. I'm thinking it's a package issue as it doesn't occur with mysql but does with mysql2. – bluewave41 Feb 10 '21 at 20:26
  • Can you add your package.json dependencies (specifically the version number of mysql2 from each). I'm also interested in the table definitions. – Bradley Feb 10 '21 at 20:48
  • It does seem to have something to do with the database versions. I got MySQL 8.0.23 setup and suddenly the data is coming back parsed. – bluewave41 Feb 10 '21 at 21:00

0 Answers0