获取 PostgresSQL 42703 错误(无效列错误)
Getting PostgresSQL 42703 error (invalid column error)
我正在使用 PostgreSQL 数据库开发一个 React 项目,这是我第一次使用它,查询特定列时出现 42703 错误。
下面是我写的查询代码
const getList = (userId) => {
return new Promise(function (resolve, reject) {
pool.query(`SELECT items FROM public."user" where id=${userId}`, (error, results) => {
if (error) {
reject(error)
}
resolve(results);
})
})
}
我已经定义了这个 getList 函数,然后我正在调用 api 来通过像这样传递 userId 来调用这个函数
app.get(`/expenses`, verifySession(), async (req, res) => {
const userId = req.session.userId;
database.getList(userId)
.then(response => {
res.status(200).send(response);
})
.catch(error => {
res.status(500).send(error);
})
})
我什至试过直接传递 userId,如下所示,它仍然给我同样的错误,这可能意味着我以错误的方式查询
app.get(`/expenses`, verifySession(), async (req, res) => {
//const userId = req.session.userId;
database.getList('17a6dea6-a63e-4da7-9910-df7eddb672e6')
.then(response => {
res.status(200).send(response);
})
.catch(error => {
res.status(500).send(error);
})
})
只有当我直接在查询中写入字符串时,它才能像这样正常工作
const getList = (userId) => {
return new Promise(function (resolve, reject) {
pool.query(`SELECT items FROM public."user" where id='17a6dea6-a63e-4da7-9910-df7eddb672e6'`, (error, results) => {
if (error) {
reject(error)
}
resolve(results);
})
})
}
谁能帮我们解决到底出了什么问题,以及我的语法是否正确?
这是我调用 api 的前端部分。
function getDataForUser() {
fetch(`http://localhost:3001/data`)
.then(response => {
return response.json();
}).then(data => {
console.log(data.rows[0]);
})
}
出现此问题是因为您在查询中没有对字符串类型使用单引号。当使用 where id=${userId}
并使用 17a6dea6-a63e-4da7-9910-df7eddb672e6 调用时转换为 where id=17a6dea6-a63e-4da7-9910-df7eddb672e6
,这会产生问题。
您可以使用两种场景来处理:
- 字符串类型使用单引号:
const getList = (userId) => {
return new Promise(function (resolve, reject) {
pool.query(`SELECT items FROM public."user" where id='${userId}'`, (error, results) => {
if (error) {
reject(error)
}
resolve(results);
})
})
}
- 使用参数绑定(默认为转换类型)
const getList = (userId) => {
return new Promise(function (resolve, reject) {
pool.query(`SELECT items FROM public."user" where id=`, [userId], (error, results) => {
if (error) {
reject(error)
}
resolve(results);
})
})
}
我正在使用 PostgreSQL 数据库开发一个 React 项目,这是我第一次使用它,查询特定列时出现 42703 错误。
下面是我写的查询代码
const getList = (userId) => {
return new Promise(function (resolve, reject) {
pool.query(`SELECT items FROM public."user" where id=${userId}`, (error, results) => {
if (error) {
reject(error)
}
resolve(results);
})
})
}
我已经定义了这个 getList 函数,然后我正在调用 api 来通过像这样传递 userId 来调用这个函数
app.get(`/expenses`, verifySession(), async (req, res) => {
const userId = req.session.userId;
database.getList(userId)
.then(response => {
res.status(200).send(response);
})
.catch(error => {
res.status(500).send(error);
})
})
我什至试过直接传递 userId,如下所示,它仍然给我同样的错误,这可能意味着我以错误的方式查询
app.get(`/expenses`, verifySession(), async (req, res) => {
//const userId = req.session.userId;
database.getList('17a6dea6-a63e-4da7-9910-df7eddb672e6')
.then(response => {
res.status(200).send(response);
})
.catch(error => {
res.status(500).send(error);
})
})
const getList = (userId) => {
return new Promise(function (resolve, reject) {
pool.query(`SELECT items FROM public."user" where id='17a6dea6-a63e-4da7-9910-df7eddb672e6'`, (error, results) => {
if (error) {
reject(error)
}
resolve(results);
})
})
}
谁能帮我们解决到底出了什么问题,以及我的语法是否正确?
这是我调用 api 的前端部分。
function getDataForUser() {
fetch(`http://localhost:3001/data`)
.then(response => {
return response.json();
}).then(data => {
console.log(data.rows[0]);
})
}
出现此问题是因为您在查询中没有对字符串类型使用单引号。当使用 where id=${userId}
并使用 17a6dea6-a63e-4da7-9910-df7eddb672e6 调用时转换为 where id=17a6dea6-a63e-4da7-9910-df7eddb672e6
,这会产生问题。
您可以使用两种场景来处理:
- 字符串类型使用单引号:
const getList = (userId) => {
return new Promise(function (resolve, reject) {
pool.query(`SELECT items FROM public."user" where id='${userId}'`, (error, results) => {
if (error) {
reject(error)
}
resolve(results);
})
})
}
- 使用参数绑定(默认为转换类型)
const getList = (userId) => {
return new Promise(function (resolve, reject) {
pool.query(`SELECT items FROM public."user" where id=`, [userId], (error, results) => {
if (error) {
reject(error)
}
resolve(results);
})
})
}