gpt4 book ai didi

Node.js 和 PostgresQL 与 Express

转载 作者:行者123 更新时间:2023-11-29 13:11:37 27 4
gpt4 key购买 nike

我编写了一段代码,使用 express 和 NODE.js 从 postgres 数据库中获取数据。数据库已与后端代码连接,但仍显示错误,如关系未退出。请检查我得到的代码和错误:

var express = require('express');
var pg = require("pg");
var app = express();

var connectionString = "postgres://postgres:postgres@localhost:5432/testDatabase?currentSchema=testSchema";

app.get('/', function(req, res, next) {
pg.connect(connectionString, function(err, client, done) {
if (err) {
console.log("Not able to get Connection " + err);
res.status(400).send(err);
} else {
console.log("Connected.!");
}

const
query = {
// give the query a unique name
name : 'fetch-user-details',
text : 'SELECT * FROM loginTable',
}

client.query(query, function(err, result) {
done(); // closing the connection;
if (err) {
console.log(err);
res.status(400).send(err);
}
res.status(200).send(result.rows);
});
});
});

app.listen(4000, function() {
console.log('Server is running.. on Port 4000');
});

我得到的错误响应是这样的:

Server is running.. on Port 4000
Connected.!
{ error: relation "logintable" does not exist
at Connection.parseE (C:\Users\sumit_srivastava\Desktop\Java_WorkSpace\SimpleNodeAppln\node_modules\pg\lib\connection.js:554:11)
at Connection.parseMessage (C:\Users\sumit_srivastava\Desktop\Java_WorkSpace\SimpleNodeAppln\node_modules\pg\lib\connection.js:381:17)
at Socket.<anonymous> (C:\Users\sumit_srivastava\Desktop\Java_WorkSpace\SimpleNodeAppln\node_modules\pg\lib\connection.js:117:22)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:594:20)
name: 'error',
length: 120,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: '15',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'src\\backend\\parser\\parse_relation.c',
line: '986',
routine: 'parserOpenTable' }

最佳答案

只需在我的 Postgres 上运行这个数据库转储

CREATE SCHEMA "testSchema"; 
ALTER SCHEMA "testSchema" OWNER TO postgres;
SET search_path = "testSchema", pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
CREATE TABLE "loginTable" ( "ID" numeric NOT NULL, "Name" text );
ALTER TABLE "loginTable" OWNER TO postgres;

这就是问题所在

将您的查询更改为此

 query = {
// give the query a unique name
name : 'fetch-user-details',
text : `SELECT * FROM "testSchema".logintable`,
}

您应该提及架构名称

尝过

  • Ubuntu
  • postgres 10.6
  • PG: 6.4.2
  • Node v10.14.1

关于Node.js 和 PostgresQL 与 Express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53942252/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com