【发布时间】:2022-01-25 13:06:54
【问题描述】:
我想从 Postgres 数据库中获取数据,并使用 Node JS
将该数据传输到 Vue 前端在这里,我创建了单独的函数来获取数据。这是我的函数定义。
function fetchshop(){
pool.query('SELECT * FROM shops',(err, shps) =>{
if (err){
throw err
}
else {
shopdetails=shps.rows;
console.log(shopdetails) // Here the data is printed in console
return shopdetails;
}
});
}
我可以在控制台中从pool.query 部分打印数据行,但是在函数调用部分,当我尝试在控制台中打印返回的数据时,它显示了undefined。这是我的函数调用代码
events=[];
shopdetails=[];
app.get("/home",async (request,response,err)=>{
events = fetchshop();
console.log(events) // This prints 'undefined' in console
response.send(events); // I want to send this events.
})
【问题讨论】:
标签: node.js postgresql function vue.js return-value