我在搜索数据库时遇到错误,但我不知道如何解决



我正在向我的数据库发出请求,我将函数设置为异步并等待,但它仍然返回未定义或承诺{pending}当我有结果时,我如何返回它?

export const getGerente = async (req, res) => {
var query = "SELECT * FROM inventory;"
const r = await select(query)
console.log(r)
return res.json({message:"teste"})
}
export async function select(query)  {
var teste = await client.connect(() =>{
client
.query(query)
.then((resultado) => {
console.log('sucess!!');
return resultado.rows
/*
const rows=resultado.rows
rows.map(x =>{
console.log(x.name)
})*/
})
.catch((erro) => {
console.log("erro: " + erro.message);
})
.then((teste) => {
console.log('Finished execution, exiting now');
process.exit();
});
})
}

result: Promise {pending}我打电话给她有个要求

您的select函数没有正确等待client.connect

试试select函数-

export async function select(query)  {
const promisifiedRows = new Promise((resolve, reject) => {
client.connect((err) => {
if (err) {
reject(err);   // err in connecting
} else {
console.log('Connected!');
client.query(query, (err, rows) => {
if (err) {
reject(err);   // err while exceuting the query
} else {
resolve(rows);
}
});
}
});
});
const rows = await promisifiedRows();
return rows;
}

相关内容

  • 没有找到相关文章

最新更新