我一直在关注这个视频https://www.youtube.com/watch?v=ufdHsFClAk0和在32:54当家伙得到结果时,我得到了一个错误ReferenceError:用户未定义。下面是我的代码
const {Client} = require('pg')
const users = [{ id: 1 }]
const client = new Client({
user: "postgres",
password: "testing",
host: "BlackPearl",
port: 5432,
database: users
})
execute()
async function execute(){
await client.connect()
console.log("Connected")
const results = await client.query("select * from users")
console.table(results.rows)
await client.end()
console.log("Disconnected")
}
下面是我在pgAdmin4
中看到的内容这是我的package。json
{
"name": "expr",
"version": "1.0.0",
"description": "",
"main": "experiment.js",
"scripts": {
"test": "node experiment.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"pg": "^8.7.1"
}
}
在终端i写npm run test
改变这个:
const client = new Client({
user: "postgres",
password: "testing",
host: "BlackPearl",
port: 5432,
database: users
})
:
const client = new Client({
user: "postgres",
password: "testing",
host: "BlackPearl",
port: 5432,
database: 'users'
})
数据库名称应该是一个字符串,而不是一个引用,如果它被称为不同的(不是users
),使用正确的名称