node.js - Node.js 和 PostgresSQL 与 Express(插入查询)
问题描述
我编写了一个代码来使用 express 和 NODE.js 从 postgres 数据库中获取数据。数据库已与后端代码连接,但仍以 json 格式显示错误。请检查我得到的代码和错误:错误就像:
{
"name": "error",
"length": 120,
"severity": "ERROR",
"code": "42703",
"position": "60",
"file": "src\\backend\\parser\\parse_relation.c",
"line": "2892",
"routine": "errorMissingColumn"
}
我写的代码是:
pg.connect(connectionString, function(err, client, done) {
if (err) {
console.error("Not able to get Connection " + err);
res.status(400).send(err);
} else {
console.log("Connected.!");
}
// Insert Data in logintable query
const
insertQuery = {
// give the query a unique name
name : 'insert-user-details',
text : 'INSERT INTO "testSchema".logintable (id, name) VALUES (03, "Anything")',
}
client.query(insertQuery, function(err, result) {
done(); // closing the connection;
if (err) {
console.error("\nError : " + err);
res.status(400).send(err);
} else {
console.log("1 record inserted");
res.status(200).send(result.rows); // this line returns only a
// table data in json
// format.
// res.status(200).send(result); // this line returns the rows
// also with the table and field information.
}
});
解决方案
我得到了上面的答案,它得到了工作。在这里检查:
//Insert Query
client.query('INSERT INTO "testSchema".logintable (id, name) VALUES ($1, $2)', [06, 'Ganesh'], function(err, result) {
if (err) {
console.error("\nError : " + err);
res.status(400).send(err);
} else {
console.log("1 record inserted");
res.status(200).send(result.rows);
}
});
推荐阅读
- php - 如何在 php 中显示数据库中的登录数据而不显示其他数据库中的数据?
- rest-assured - RestAssured LogConfig.blacklistedHeaders 错误
- python - 通过 Python 连接电报机器人的问题
- reactjs - 材料 UI 组件的 OnClick 在 React Js 中不起作用
- r - ggsurvplot 在最后一天截断曲线
- python - Python将每个字符串从数组附加到一个txt文件
- javascript - 如何在 CSS 多列布局中交换列和行?
- javascript - 如何禁用列表项?
- kotlin - 为什么我的 FloatingActionButton 在 Kotlin 中的 Fragment 上不起作用?
- oracle - Oracle 中的 NOPARALLEL 和 PARALLEL 1 有什么区别?