简体   繁体   中英

check if object in array of objects - javascript

I know i have to use some but for some reason i cant seem to get it right. i have a collection in my mongodb database of posts. each post has an array of objects named "likes" that references the users that liked this post. so in my backend i want to check if the user exists in the likes array of the post. if it does not exist then like the post, else return with an appropriate message on my react frontend. The code i will include always returns false from some so a user can like a post infinite times.

exports.postLike = async (req, res, next) => {
  const postId = req.query.postId;
  const userId = req.query.userId;


  console.log('postId: ' + postId);

  try{
    const post = await Post.findById(postId).populate('creator').populate('likes');
    const user = await User.findById(userId);
    if (!post.likes.some(post => post._id === user._id)){
      post.likes.push(user);
      console.log('liked a post');
      const result = await post.save();
      res.status(200).json({ message: 'Post liked!', post: result });
    } else {
      console.log('Post already liked!');
      res.status(200).json({ message: 'Post already liked!', post: post });
    }

  }catch (err) {
    if (!err.statusCode) {
      err.statusCode = 500;
    }
    next(err);
  }

};

i clearly haven't understood, yet, how some works so if you can help that would be great. also if you have any other solution that would be good in this case then please post it. i tried some random codes with indexOf and includes for checking but it didn't work either. i am not sure which is the right way to check if the user object is included in the "likes" array of objects. i would prefer not to write any function of my own to check this, i want to do it using an existing function/method provided by javascript.

MongoDB.ObjectId is a wrapper around a primitve, just like Number or Boolean . And just like

  new Boolean(true) === new Boolean(true)

will be false, your comparison will fail too. You have to take out the primitive for comparison:

 post._id.valueOf() === user._id.valueOf()

Going to offer a different route here. You are fetching all the data including a join to the creator and likes just to add a like to the collection. This is a little wasteful and can be achieved by just doing an update and use $addToSet which will add the like if it does not exist.

You then just check nModified in the result to know if it was added or not. So you can have:

const result = await Post.updateOne(
    {
        id: 1
    },
    {
        $addToSet: {
            likes: {
                userId: mongoose.Types.ObjectId(req.query.userId)
            }
        }
    }
);

console.info(result.nModified === 1);

Alternatively, you can use some as follows using === to compare type and value:

posts.likes.some(like => like.userId.toString() === req.query.userId)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM