在我的 ejs 文件中,我在另一个文件中调用返回 mysql 结果的辅助函数。问题是即使我使用 .then(),返回值也是 [ object Promise ]。我不确定为什么。
辅助函数
var conn = require("../routeFunctions/mySqlConn");
//get the amount of likes
var 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>
鸿蒙传说
相关分类