密码正确,密码错误
代码本身是对的!抛出错误:
- 如果您根本没有设置密码,或者
- 如果您输入了错误的密码。例如,我不小心从指南中复制了“root”作为密码,尽管我将
my_user 的密码设置为“postgres”。当其他一切都已经在运行并且只有密码错误时,您会收到同样的错误。
详细错误:
myuser@mypc:~/project/node-postgres$ node index.js
App running on port 3001.
/home/myuser/myproject/node-postgres/merchant_model.js:17
resolve(results.rows);
^
TypeError: Cannot read property 'rows' of undefined
at pool.query (/home/myuser/myproject/node-postgres/merchant_model.js:17:23)
at PendingItem.connect [as callback] (/home/myuser/myproject/node-postgres/node_modules/pg-pool/index.js:363:16)
at Client.client.connect [as _connectionCallback] (/home/myuser/myproject/node-postgres/node_modules/pg-pool/index.js:246:23)
at Client._handleErrorWhileConnecting (/home/myuser/myproject/node-postgres/node_modules/pg/lib/client.js:305:19)
at Client._handleErrorMessage (/home/myuser/myproject/node-postgres/node_modules/pg/lib/client.js:325:19)
at Connection.emit (events.js:198:13)
at parse (/home/myuser/myproject/node-postgres/node_modules/pg/lib/connection.js:114:12)
at Parser.parse (/home/myuser/myproject/node-postgres/node_modules/pg-protocol/dist/parser.js:40:17)
at Socket.stream.on (/home/myuser/myproject/node-postgres/node_modules/pg-protocol/dist/index.js:11:42)
at Socket.emit (events.js:198:13)
我可以按照Getting started with Postgres in your React app: an end to end example 的指南找到解决方案,您还可以在此处找到有关此处使用的代码的更多信息。为了更深入地研究这一点,How to quickly build an API using Node.js & PostgreSQL 也可能会有所帮助。要对此进行测试,您需要安装 PostgreSQL(或使用 Web 服务器,但随后您将需要 SSL,请参阅 How to create a pool object in javascript that can connect to a database backend using SSL?)并按照指南中的操作在数据库中创建“Merchant”表。
由于本教程的文件没有任何错误,因此问题的问题很可能发生(如我的情况),因为您没有完全遵循有关如何设置数据库角色和密码的指南。你可以通过运行检查你的设置是否正确,这取决于你选择了向导的my_user还是标准的postgres用户进行连接(如果是后者,请将merchant_model.js中的池设置更改为postgres用户) :
psql -d my_database -U my_user
或:
psql -d my_database -U postgres
它应该要求输入密码:
Password for user postgres/my_user:
如果您的密码尝试不被接受(标准测试密码通常是postgres 或指南使用root),您可能没有设置任何密码,很简单。这是我的情况,因为 PostgreSQL 的全新安装没有给超级用户 postgres 一个密码,你创建的任何其他角色也没有!它只是没有引起我的注意,因为我一直在使用
sudo su postgres
连接,它只要求您输入 Linux 用户的密码,而不是 postgres 用户的密码。
因此:您必须使用有密码的postgres(或my_user)用户。您需要关注 Getting error: Peer authentication failed for user "postgres", when trying to get pgsql working with rails 才能给 postgres 一个密码,如果您想使用像 my_user 这样的其他用户,也请给它一个密码。
只有这样,您才能完成连接。然后错误就会消失。
展望
当您从存储库中添加了这两个文件时:
- merchant_model.js(来自指南或上述精简版的那个)
- index.js(来自指南)
到“node-postgres”目录,在项目目录下,你可以用node index.js运行它。
如果运行,您应该会看到
App running on port 3001.
在命令提示符下,命令仍在运行。然后打开浏览器输入localhost:3001查看后台:
当您随后按照指南添加存储库中的文件时:
到“react-postgres\src”目录下,可以运行:
npm start
查看前端:
试试Add按钮:
PS:编码不好?可能不会!
第一个答案是错误的,即所谓的“错误编码”是错误的原因。 您可以使用指南的 github 存储库的完整副本,一切都会正常运行,因为错误最终来自其他方面。然而,上面的代码可能是错误的编码,至少count null entries in database column in a RESTfull way 也使用了建议的await。我怀疑这很糟糕,因为它运行良好,当我后来在 react-postgres 上运行没有 Promises 的代码时,我隐约记得有一条消息说你应该使用错误处理程序。然而,对于任何感兴趣的人,这里是第一个答案的建议付诸实践,它有效,“行”错误消失了,但这只是因为“行”不再在代码中:)))。
merchant_model.js:
const Pool = require('pg').Pool
const pool = new Pool({
user: 'my_user',
host: 'localhost',
database: 'my_database',
password: 'postgres',
port: 5432,
});
const getMerchants = () => pool.query('SELECT * FROM Merchants ORDER BY id ASC')
const createMerchant = (body) => {
const { name, email } = body
pool.query('INSERT INTO Merchants (name, email) VALUES ($1, $2) RETURNING *', [name, email])
}
const deleteMerchant = () => {
const id = parseInt(request.params.id)
pool.query('DELETE FROM Merchants WHERE ID = $1', [id])
}
module.exports = {
getMerchants,
createMerchant,
deleteMerchant,
}
是否使用带有或不带有 Promises 的代码取决于您,也许应该坚持使用指南并将其与 Promises 一起使用。