| 0 | kirasiris | Categories: Tags: , , , , ,

beFree current's API field

A bit of intro

Usually when creating complex websites such as an e-commerce, social network or streaming site, you will probably end up in the position of finding inspiration in some of the functions big companies built with the mentioned structures share; Facebook and YouTube let you like/dislike X post after triggering certain icon such is the thumbs-up, am I right? Well, what happens after you have clicked on it? Does the icon stays the same or is it a new icon the one being shown? maybe the same icon but with a different color?. This is done by updating a field in a specific Id.

Well,  the people who have liked the same post as you and you – are – probably the only ones able to see the new icon. The rest of the accounts do not. This is due to a (Boolean. Probably)function that updates the post by adding an object in which your account is being added to and then when you visit the same post again, the same function checks if you’re already included in the array and if  you’ve been found, it shows the new icon, letting you know that you’ve liked X post. Confusing, don’t you think?.

Let’s begin

Today, I will show you how to do something similar with NodeJS and MongoDB. First we will talk about updating likes and then we will continue with a second function to update a field in a specific document. All of this without having to send data from any form. That’s the reason why I say it’s a Boolean function, you call it by triggering certain action in your front end app!.

The likes is the complex one as you will have to run through the whole array of objects which can sometimes take up to .98 miliseconds(just an assumption; not to be taken seriously). This is how an array of objects looks like and before you ask, I just put one object for the sake of the post and because I know its not that complex to understand.

"likes": [
  {
      "_id": "5e1151e5aa6a092abcff1128",
      "user": "5e0d1b9315a8f23df855e4c3"
  }
],

Now, we always start off by calling out X post from DB using the findById method. From there we move on into more complex logic.

// Check if the product has already been liked
const product = await Model.findById(req.params.id);
if (product.likes.filter(like => like.user.toString() === req.user.id).length > 0) {
  // If already liked, then dislike it
  const removeIndex = product.likes.map(like => like.user.toString()).indexOf(req.user.id);
  product.likes.splice(removeIndex, 1);
  await product.save();

  res.status(201).json({ success: true, data: product.likes });
} else {
  // Add following to current loggedIn User
  product.likes.unshift({ user: req.user.id });

  await product.save();
  res.status(201).json({ success: true, data: product.likes });
}

Let’s explain it with baby-steps.

We’re doing an if-statement which verifies if an object matches with the current loggedIn – req.user.id; you – user within the array of objects, likes(product.likes). If found run(map) individually through each single object and remove(splice) the one in which your account matches from the array of objects. Else if your account is not found in the array, then proceed and add(unshift) it to the array!. Happy ending!.

Secondly, the easy one; updating a field value from an object.

const product = await Model.findById(req.params.id);
product.isHidden === 'yes' ? (product.isHidden = 'no') : (product.isHidden = 'yes');
await product.save();

The previous object looks exactly like this:

"hidden": "no",

Too simple, right?. That’s all, everytime you trigger X action you will be switching from yes -to – no and vice-versa!.

Leave a Reply

Back to Top