【发布时间】:2021-01-21 00:03:58
【问题描述】:
我正在做一个项目,我的母语是西班牙语,在我的数据库中,我使用诸如“ñ”和“é”之类的字符。当我使用 psql shell 时,这些字符正确显示在终端上,但是当我使用 node-postgress 进行查询时,这些字符没有显示出来,而是得到 ¤ 或 ¢。
在我的数据库中,我将 client_encoding 和 server_encoding 都设置为 UTF8,我什至使用查询检查了 node-postgress,以查看它们是否也设置为 UTF8,并且由于其他原因它们没有更改。
p>我的数据库连接是这样设置的
const { Pool } = require("pg");
const db = new Pool({
user: user,
password: password,
host: localhost,
port: 5432,
database: my_database,
});
module.exports = db;
我的查询代码是这样的:
const router = require("express").Router(),
db = require("../database/db");
//GET A PLACE ROUTE
router.get("/", async (req, res) => {
try {
const place = await db.query("SELECT * FROM places");
console.log(place.rows[0].name);
res.status(200).json({
status: "success",
data: {
place_name: place.rows[0].name,
},
});
} catch (error) {
console.error(error.message);
res.status(500).send("Error del servidor");
}
});
现在,如果该地点的名称是“Salón de peñas”,那么它将如何显示在 console.log 和我的 json 响应中,就像“Sal¢n de pe¤as”一样。
起初,我认为问题是因为我没有正确设置我的 json 响应字符集,但后来我将这些字符作为响应发送,并且它们正确显示。问题是当这些字符来自我的数据库时。我检查了数据库编码(客户端和服务器),它们都设置为 UTF8,就像我之前说的,当我使用 psql shell 时,这些字符可以正确显示。
我基本上遇到了与this 没有得到答案的问题完全相同的问题
【问题讨论】:
标签: node.js postgresql express utf-8 node-postgres