我有两个查询,一个用于检查用户当前是否关注帖子作者,另一个用于检查用户是否已经喜欢该帖子。
我正在使用类似:if((likes)&&(resault)来渲染具有正确对象的页面,但它似乎不起作用。
我花了一些时间试图解决这个问题,所以我发布了一个很长的代码,请耐心等待:
if (req.user) { // if the user is currently logged in
var q = 'SELECT * FROM follows WHERE followee_id =\'' + author_id + '\' AND follower_id =\'' + req.user.id + '\'';
connection.query(q, function (err, resault) {
if (err) {
console.log(err);
}
var isfollowing = {
follower_id: resault[0].follower_id,
followee_id: resault[0].followee_id
};
var q = 'SELECT * FROM likes WHERE user_id =\'' + req.user.id + '\' AND post_id =\'' + post_id + '\'';
connection.query(q, function (err, likes) {
if (err) {
console.log(err);
}
var hasLikes = {
user_id: likes[0].user_id,
post_id: likes[0].post_id
};
if ((likes.length > 0) && (resault.length > 0)) { // if the user has likes and is following users
console.log('has likes, has follows');
res.render('single', {
showPost: post,
comments: comments,
numComments: numComments,
hasLikes: hasLikes,
likes: likes,
isfollowing: isfollowing,
resault: resault
});
} else if ((likes.length < 0) && (resault.length > 0)) {
res.render('single', {
showPost: post,
comments: comments,
numComments: numComments,
isfollowing: isfollowing,
resault: resault,
likes: likes
});
console.log('NO likes, has follows');
} else if ((likes.length > 0) && (resault.length < 0)) {
console.log('has likes, NO follows');
res.render('single', {
showPost: post,
comments: comments,
numComments: numComments,
hasLikes: hasLikes,
resault: resault,
likes: likes
});
} else {
console.log('has nothing');
res.render('single', {
showPost: post,
comments: comments,
numComments: numComments,
resault: resault,
likes: likes
});
}
console.log(isfollowing);
console.log(hasLikes);
});
});
}
完整路线/代码:
app.get("/single/:id", function(req, res){
var post_id = req.params.id;
var q = "SELECT *, DATE_FORMAT(posts.created_at, '%m/%d/%Y') AS created_at FROM posts INNER JOIN users ON posts.user_id=users.id WHERE posts.id =" + post_id;
connection.query(q, function(err, results){
if (err) throw err;
//render post
var post_title = results[0].post_title;
var image = results[0].image;
var post_description = results[0].post_description;
var username=results[0].username;
var bio = results[0].bio;
var requests = results[0].requests;
var author_id = results[0].user_id;
//store all in one variable
var post = {post_title:post_title, image:image, post_description:post_description,post_id:post_id,username:username, bio:bio, avatar:results[0].avatar, requests:requests, author_id:author_id, post_date:results[0].created_at};
var q = "SELECT *, DATE_FORMAT(comments.created_at, '%m/%d/%Y') AS created_at FROM comments INNER JOIN users ON comments.user_id=users.id WHERE post_id =" + post_id;
connection.query(q, function(err, comments){
if (err) throw err;
var numComments = comments.length;
if(req.user){ // if the user is currently logged in
var q ="SELECT * FROM follows WHERE followee_id ='" + author_id + "' AND follower_id ='" + req.user.id +"'";
connection.query(q, function(err, resault){
if(err){
console.log(err);
}
var isfollowing = {follower_id:resault[0].follower_id, followee_id:resault[0].followee_id};
var q ="SELECT * FROM likes WHERE user_id ='" + req.user.id + "' AND post_id ='" + post_id +"'";
connection.query(q, function(err, likes){
if(err){
console.log(err);
}
var hasLikes = {user_id:likes[0].user_id, post_id:likes[0].post_id};
if ((likes.length > 0) && (resault.length > 0)) { // if the user has likes and is following users
console.log("has likes, has follows")
res.render("single",{showPost:post, comments:comments, numComments:numComments, hasLikes:hasLikes, likes:likes, isfollowing:isfollowing, resault:resault});
} else if((likes.length < 0) && (resault.length > 0)){
res.render("single",{showPost:post, comments:comments, numComments:numComments, isfollowing:isfollowing, resault:resault, likes:likes});
console.log("NO likes, has follows")
} else if ((likes.length > 0) && (resault.length < 0) ){
console.log("has likes, NO follows")
res.render("single",{showPost:post, comments:comments, numComments:numComments, hasLikes:hasLikes, resault:resault, likes:likes});
} else {
console.log("has nothing")
res.render("single",{showPost:post, comments:comments, numComments:numComments, resault:resault, likes:likes});
}
console.log(isfollowing);
console.log(hasLikes);
});
});
} else { // if user not logged in
res.render("single",{showPost:post, comments:comments, numComments:numComments});
}
我开始认为这不是最好的方法吗?有小费吗? 谢谢
答案 0 :(得分:0)
发现我的错误。
我使用的是:
likes.length < 0
检查数组是否为空,我应该使用它:
!likes.length