【问题标题】:Who to follow suggestions function in Nodejs?谁在 Nodejs 中遵循建议功能?
【发布时间】:2020-10-09 21:15:03
【问题描述】:

所以我的模型用户有两个看起来完全像这样的字段:

following: [
  {
    friendship: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Friendship'
    },
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User'
    }
  }
],
followers: [
  {
    friendship: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Friendship'
    },
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User'
    }
  }
]

我的功能应该“建议”给已登录帐户的用户关注。这是函数:

exports.whoToFollow = asyncHandler(async (req, res, next) => {
  const people = await User.find()
    .merge({
      _id: { $nin: req.user._id },
      followers: { $nin: { user: req.user._id } }
    })
    .select(`username avatar name`)
    .limit(6);
  res.status(200).json({ success: true, data: people });
});

合并的第一行是为了避免显示登录帐户,之后的行是我遇到问题的地方。 它应该给我我还没有关注的用户,但它总是显示我已经关注的用户,有点混乱哈哈。

有人知道怎么解决吗?

【问题讨论】:

    标签: javascript node.js reactjs mongodb mongoose


    【解决方案1】:

    我终于解决了!!:

    exports.whoToFollow = asyncHandler(async (req, res, next) => {
      const people = await User.find()
        .merge({
          _id: { $nin: req.user._id },
          'followers.user': { $nin: req.user._id }
        })
        .select(`username avatar name`)
        .limit(6);
      res.status(200).json({ success: true, data: people });
    });
    

    我只需要使用点符号哈哈。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      相关资源
      最近更新 更多