在我的 ejs 文件中,我在另一個文件中調用返回 mysql 結果的輔助函數。問題是即使我使用 .then(),返回值也是 [ object Promise ]。我不確定為什么。輔助函數 var conn = require("../routeFunctions/mySqlConn");//get the amount of likesvar getLikesCountEjs = function(idOfPost) { var query = getLikeCount(idOfPost).q; var preparedStatement = getLikeCount(idOfPost).ps; return new Promise(function (resolve, reject) { conn.query(query, preparedStatement, function (err, result, fields) { if (err) { reject("problem getting likes count"); } else { resolve(result[0].count); } }); }); } //get all likes count function getLikeCount(idOfPost) { var query = "SELECT COUNT(*) as count FROM likes where idOfPost = ?"; var preparedStatement = [parseInt(idOfPost)]; return { ps: preparedStatement, q: query }; } //you can return multiple functions module.exports = { getLikesCountEjs : getLikesCountEjs };我在其中調用函數的 ejs 文件 <a href="#" class="link-black text-sm interactSetLikesTimeLine" id = "setLikes-<%=posts.id%>"> <i class="far fa-thumbs-up mr-1 initalLoadSetLikes"> Likes <%= asyncFunctions.getLikesCountEjs(posts.id).then(function(result){ console.log(result); }) %> </i> </a>
在 ejs 中使用 .then 和輔助函數返回 [ object Promise ]
紫衣仙女
2023-09-07 14:37:41