I use restful API with express & node.js and want to encode data to utf-8.
I install utf8 with
npm install utf8I set
const utf8 = require('utf8');I use
utf8.encode(string)to object like that:pool.query(`SELECT Station, Ime FROM auto_q_stations;`, function (error, result2, fields) { if (error) return res.status(500).json({ error: "Грешна заявка. Опитай отново !" }) HQstationsAHS = result2; res.json({ HQstationsAHS }) });});
I receive error like that:
TypeError: string.charCodeAt is not a function
How to encode object in node.js using express (restful api) ?
My problem is the following, when I execute a query in a database, all data in Bulgarian are OK.
When I execute the query in node.is & express environment I receive the data like that:
"������-��������"
Please help, I really don't know how to fix this problem.
CodePudding user response:
You should loop through your array and change every "Ime" field. Something like
pool.query(`CALL Get_Discharge_Station('${dateNow}', ${daysBefore}, ${stNumber})`, function (error, result, fields) {
if (error)
return res.status(500).json({ error: "Грешна заявка. Опитай отново !" })
HQstationsAHS = result2.map((item) => {
item.Ime = utf8.encode(item.Ime);
return item;
});
res.json({ HQstationsAHS })
});
