我收到此错误 -- TypeError:无法读取未定义的属性“行”,这是怎么回事?这可能是我遗漏的很小的东西。 getMerchants 和 resolve(result.rows) 出现错误
const Pool = require('pg').Pool
const pool = new Pool({
user: 'my_user',
host: 'localhost',
database: 'my_database',
password: 'root',
port: 5432,
});
const getMerchants = () => {
return new Promise(function(resolve, reject) {
pool.query('SELECT * FROM userIds ORDER BY id ASC', (error, results) => {
if (error) {
reject(error)
}
resolve(results.rows);
})
})
}
const createMerchant = (body) => {
return new Promise(function(resolve, reject) {
const { name, email } = body
pool.query('INSERT INTO userIds (name, email) VALUES ($1, $2) RETURNING *', [name, email], (error, results) => {
if (error) {
reject(error)
}
resolve(`A new merchant has been added added: ${results.rows[0]}`)
})
})
}
const deleteMerchant = () => {
return new Promise(function(resolve, reject) {
const id = parseInt(Request.params.id)
pool.query('DELETE FROM userIds WHERE id = $1', [id], (error, results) => {
if (error) {
reject(error)
}
resolve(`Merchant deleted with ID: ${id}`)
})
})
}
module.exports = {
getMerchants,
createMerchant,
deleteMerchant,
}
答案 0 :(得分:0)
根据我的评论:
<块引用>查询的第二个参数是查询参数,不是回调函数。如果没有参数,则传入 []
。除此之外,您的代码看起来在很大程度上是多余的。您应该遵循库提供的正确异步模式,而不是重新创建所有那些无用的承诺...
const getMerchants = () => pool.query('SELECT * FROM userIds ORDER BY id ASC');
然后像这样使用它:
const {rows} = await getMerchants();