我已经成功做到了,以便用户可以彼此关注。单击“关注”时会发生什么,它将把您的用户ID推送到您单击“关注”的用户的关注者,然后将“用户”的ID推送到“当前用户关注”部分中的“关注”。
现在,我正在尝试使之进入,以便在您进入个人资料页面时,如果您不关注用户,它将显示“关注”,如果您已经关注,则显示“取消关注”。我正在尝试使其遍历用户关注者,以查看ID之一是否与当前用户ID匹配。但是,无论我做什么,我都无法获得正确的结果。
我观看用户个人资料的路线是:
router.get("/profile/:id", middleware.isLoggedIn, function(req, res) {
User.findById(req.params.id).populate("followers").exec(function(err, foundUser) {
if (err) {
req.flash("error", "Ooops! Seems like there are no users with this id.");
res.redirect("back");
}
else {
Mappin.find().where("author.id").equals(foundUser._id).exec(function(err, foundMappins) {
if (err) {
req.flash("error", "Something went wrong trying to find the map markers from this user.");
res.redirect("back");
}
else {
Blogpost.find().where("author.id").equals(foundUser._id).exec(function(err, foundBlogposts) {
if (err) {
req.flash("error", "Something went wrong trying to find the blogposts from this user.");
res.redirect("back");
}
else {
Post.find().where("author.id").equals(foundUser._id).exec(function(err, foundPosts) {
if (err) {
req.flash("error", "Something went wrong trying to find the posts from this user.");
res.redirect("back");
}
else {
res.render("webapplication/user/userprofile", { user: foundUser, mappin: foundMappins, blogpost: foundBlogposts, post: foundPosts });
}
});
}
});
}
});
}
});
});
当我尝试检查用户是否已经关注时,我正在用户视图中执行以下操作:
<% user.followers.forEach(function(follower) { %>
<% if(currentUser && follower._id.equals(currentUser._id)){ %>
<div class="Button-Collection">
<a href="/profile/<%= user.id %>/follow" class="Button Add" title="Unfollow <%= user.firstName %>">Unfollow</a>
</div>
<% } %>
<% if (currentUser && !follower._id.equals(currentUser._id)) { %>
<div class="Button-Collection">
<a href="/profile/<%= user.id %>/follow" class="Button Add" title="Follow <%= user.firstName %>">Follow</a>
</div>
<% } %>
<% }); %>
我的userSchema:
var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");
var UserSchema = new mongoose.Schema({
username: String,
password: String,
avatar: String,
firstName: String,
lastName: String,
email: { type: String, unique: true },
resetPasswordToken: String,
resetPasswordExpires: Date,
gender: String,
createdAt: { type: Date, default: Date.now },
comments: [ // Allows users to comment on business profiles and allows businesses to showcase their comments
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
],
posts: [ // Allows users to comment on business profiles and allows businesses to showcase their comments
{
type: mongoose.Schema.Types.ObjectId,
ref: "Post"
}
],
followers: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}],
following: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}],
blogPosts: [ // Allows admins to see and write blogposts for the website
{
type: mongoose.Schema.Types.ObjectId,
ref: "Blogpost"
}
],
mapPins: [ // Allows admins to see and write blogposts for the website
{
type: mongoose.Schema.Types.ObjectId,
ref: "Mappin"
}
],
businessLocations: String,
businessBiography: String,
businessLogo: String,
businessName: String,
businessWebsite: String,
businessFacebook: String,
businessTwitter: String,
businessYoutube: String,
businessLinkedin: String,
isAdmin: { type: Boolean, default: false },
isBusiness: { type: Boolean, default: false },
isUser: { type: Boolean, default: false },
facebookId: String,
facebookToken: String,
facebookEmail: String,
googleId: String,
googleToken: String,
googleEmail: String
});
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", UserSchema);
我也尝试过for(var i = 0; i 我真的希望你们中的一个能够帮助我解决这个难题,因为我无法自己解决这个难题。
答案 0 :(得分:0)
Michael,请尝试使用==来比较ID:
follower._id == currentUser._id