在过去的五天里,我一直在用键盘敲打我的头,试图制作一个在纸上看起来很简单的跟随器系统,但是由于我的无能,我无法解决它。
这是我的模式:
var accountSchema = new Schema({
fname: {type: String, required: true, trim: true},
lname: {type: String, required: true, trim: true},
username: {type: String, required: true, unique: true, trim: true},
bio: String,
email: {type: String, required: true, trim: true},
password: {type: String, required: true},
date: { type: Date, default: Date.now },
accountType: String,
posts: [{body:String, owner: String, comments:[{owner:String, comment:
String, date:Date}], likes:[{liker: String}], date: { type: Date,
default: Date.now }}],
following: [{username: String}],
followers: [{username: String}],
notifications: [{owner: String, date: Date, post:Number}]
});
这是我的终点
api_follow: function(req, res){
account.findOne({'username': req.session.accountid}, function(err, account){
if(err || !account){
res.send(err);
}else{
account.following.findOne({'username': req.body.following}, function(err, account){
//if username isnt found in following list add it,
//if username is found in following list remove it
});
}
});
那是我第一次尝试找到子文档,我知道它现在不正确,但是我所有其他尝试也都失败了。我正在尝试找到一个帐户,然后检查它是否已经是用户。如果该帐户已经在关注该用户,那么我们将其删除;否则,我们将其添加。然后,我将切换一些变量名,并为跟随的用户提供类似的功能,以将该帐户添加到其关注者列表中。