There is a mysql database in which records are stored in both Latin and Cyrillic. Connection to the database occurs through
const iconv = require("iconv-lite") const { MySQL } = require('mysql-promisify'); const db = new MySQL({ host: 'xxx', user: 'xxx', password:'xxx', charset: 'utf8_unicode_ci', database: 'xxx', timeout: 100, }); (async() => { var{ results } = await db.query({ sql: `SELECT * FROM xxx`, }); console.log(results)// ΠΠΈΡΠΈΠ»Π»ΠΈΡΠ΅ΡΠΊΠΈΠ΅ ΡΠΈΠΌΠ²ΠΎΠ»Ρ ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ°ΡΡΡΡ Π² Π²ΠΈΠ΄Π΅ Π²ΠΎΠΏΡΠΎΡΠΈΡΠ΅Π»ΡΠ½ΡΡ
Π·Π½Π°ΠΊΠΎΠ² Array.prototype.forEach.call(results, result=> { var message = iconv.encode(iconv.decode(result['title'], "cp1251"), "utf8").toString(); console.log(message);//ΠΡΠΈ ΡΠ°ΠΊΠΎΠΉ ΠΊΠΎΠ½Π²Π΅ΡΡΠ°ΡΠΈΠΈ Π²ΡΠ²ΠΎΠ΄ΠΈΡΡΡ Π²ΠΌΠ΅ΡΡΠΎ Π²ΠΎΠΏΡΠΎΡΠΈΡΠ΅Π»ΡΠ½ΡΡ
Π·Π½Π°ΠΊΠΎΠ² "ΡΡΡ" }); })(); At the moment in the database are fields with type TEXT and utf8_unicode_ci coding. Variations were tried different, but none helped solve the problem.
result['title']and so should be in utf8 ... why decode it from cp1251? - Fat-Zercharset: 'utf8_unicode_ci'parametercharset: 'utf8_unicode_ci'in the constructor should set the encoding of the connection, client and result. For confidence, perform (in the session in which the problem, but not in the client)SHOW SESSION VARIABLES LIKE 'character_set%';. Another possible option is that the fields were originally added to the database in the wrong encoding. - Fat-Zercharsetconstructor's paremeter is not working correctly ... or you can simply executeSET NAMESmanually after creating the connection ... - Fat-Zer