简体   繁体   中英

Troubles with encoding when getting non-english characters from database in node.js

In node js I have very simple code for making requests to database:

var mysql      = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'user',
    password: 'password',
    database: "databasename"
});

connection.connect();

connection.query('SELECT * FROM main_categories', function (error, results, fields) {
    if (error) throw error;
    console.log(results);
});

connection.end();

I'm creating database and table with this sql script:

CREATE DATABASE databasename
  CHARACTER SET utf8
  COLLATE utf8_unicode_ci;

CREATE TABLE main_categories (
  id   INT          NOT NULL AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  PRIMARY KEY (id)
);

When I'm trying to select code just in mysql console everything is looks good, but when I'm trying to get results in node.js, in console logs I see it something like this:

[ RowDataPacket { id: 1, name: 'Візочки' },
  RowDataPacket { id: 2, name: 'ДитÑчий транÑпорт' },
  RowDataPacket { id: 3, name: 'ÐвтокріÑла' },
  RowDataPacket { id: 4, name: 'Іграшки' },
  RowDataPacket { id: 5, name: 'ДитÑча кімната' },
  RowDataPacket { id: 6, name: 'Ð’Ñе Ð´Ð»Ñ Ð³Ð¾Ð´ÑƒÐ²Ð°Ð½Ð½Ñ' },
  RowDataPacket { id: 7, name: 'ÐкÑеÑуари Ð´Ð»Ñ Ð¼Ð°Ð¼' },
  RowDataPacket { id: 8, name: 'Гігієна та доглÑд' } ]

I've tried to add charset in config object when using mysql.createConnection and any other solutions but no results unfortunately.

UPD: It might be helpful to see database variables:

mysql> SHOW variables WHERE variable_name LIKE '%coll%' OR variable_name LIKE '%char%';
+--------------------------+-----------------------------------------------------------+
| Variable_name            | Value                                                     |
+--------------------------+-----------------------------------------------------------+
| character_set_client     | utf8                                                      |
| character_set_connection | utf8                                                      |
| character_set_database   | utf8                                                      |
| character_set_filesystem | binary                                                    |
| character_set_results    | utf8                                                      |
| character_set_server     | latin1                                                    |
| character_set_system     | utf8                                                      |
| character_sets_dir       | /usr/local/mysql-5.7.19-macos10.12-x86_64/share/charsets/ |
| collation_connection     | utf8_general_ci                                           |
| collation_database       | utf8_general_ci                                           |
| collation_server         | latin1_swedish_ci                                         |
+--------------------------+-----------------------------------------------------------+

Ð'ізочки is Mojibake for Візочки . It happens when you don't have utf8/utf8mb4 everywhere. In particular, node.js needs something like

var connection = mysql.createConnection({ ... , charset : 'utf8mb4'});

I've added SET NAMES utf8; before creating the table so it looks like this:

SET NAMES utf8;

CREATE TABLE main_categories (
  id   INT          NOT NULL AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  PRIMARY KEY (id)
);

And that's it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM