通过实例方法(Mongodb/mongoose)发送参数



如果我想在实例方法中发送一个参数,该怎么做?

var User = new Schema({
    following: [{ type: Schema.Types.ObjectId, ref: 'User' }],
    followers: [{ type: Schema.Types.ObjectId, ref: 'User' }]
});
User.methods.follow = function(otherUser, cb) {
  this.following.push(otherUser);
  this.save(cb);
};
User.methods.unfollow = function(otherUser, cb) {
  var index = this.following.indexOf(otherUser._id);
  if (index >= 0)
  {
    this.following.splice(index, 1);
    this.save(cb);
  }
};

我可以像那样传递"otherUser"吗?如果不是,我是否应该使用静态方法来实现这一点?

我遇到的问题是,每当我尝试使用该方法时,我得到"没有方法'跟随'"。我不知道我做错了什么....我要提一下,我是用护照做身份验证的。不确定这是否对我所看到的有影响

我看差不多。唯一的旁注是,您可以使用* this.following.pull({_id: otherUser})代替您的splice

  • 假设你正在传递用户id。

相关内容

最新更新