【发布时间】:2022-01-26 06:38:21
【问题描述】:
我在 EJS 中使用 MySQL 从数据库中获取数据(带有承诺)。当我得到数据时,我控制台.log(数据),我可以在服务器控制台中看到数据。但是当我想用 ejs 显示数据时,什么也没发生,为什么?这是我的代码:
<%
let conn = connection() //Connection is a func who returned a mysql.createConnection() with the good props
function GetDataToShow(){
return new Promise((resolve, reject) => { //Promise returned, while promise is not returned, js does not continue to execute the code if function is async and await keyword is used when this function is called
conn.query(
"SELECT * FROM articles",
(err, result) => {
return err ? reject(err) : resolve(result)
}
)
})
}
let results = [];
(async () =>{
conn.connect()
results = await GetDataToShow()
console.log(results) //Data is show in the console(it works)
conn.end()
for (let index = 0; index < results.length; index++) { %>
<h1><%= results[index].name</h1> <!--But not that !!!!!-->
<%}
console.log("Happy coding !")
})()
%>
【问题讨论】:
-
来自“console.log(results)”的打印在哪里?
-
我已经编辑了我的问题以获得更多理解
标签: javascript mysql node.js ejs