简体   繁体   中英

TypeError: string.charCodeAt is not a function. How to encode object in node.js using express (restful api)

I use restful API with express & node.js and want to encode data to utf-8.

  1. I install utf8 with npm install utf8

  2. I set const utf8 = require('utf8');

  3. 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.

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

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