【发布时间】:2019-07-12 06:10:07
【问题描述】:
我已经成功做到了,这样用户就可以互相关注了。当您点击关注时会发生什么情况,它会将您的用户 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>
<% } %>
<% }); %>
我的用户架构:
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
我真的希望你们中的一个人能够帮助我解决这个谜题,因为我无法自己解决这个问题。
【问题讨论】:
-
你能发给你
User架构吗?还要检查编辑以防关闭</div>是您的问题的一部分。 -
回调helllllllllllllllllllllllllllllllllllll! spp>
-
@dimitristseggenes - 我现在也添加了我的用户架构。
标签: node.js mongodb loops express ejs