user8463989
user8463989

Reputation: 2459

What json response to send if count total is equal to zero

I want to display some data only if the total is greater than zero. And I am using axios and want to send back a json response. So, the <li> should only show if there is a value greater than 0 otherwise nothing should show. If I don't send back a response or I have just res.status(200) then in the network tab I can see that it just stays in a pending state with provisional headers shown, and eventually an ERR_EMPTY_RESPONSE message.

exports.getPendingCountSide = (req, res, next) => {
  Product.countDocuments({ userId: req.user, status: "pending" })
    .then(pending => {
      if (pending > 0) {
        const pendingTotal =
          '<li><a href="/account/pending">Pending <span class="nav-tag yellow">' +
          pending +
          "</span></a></li>";
        res.status(200).json({ html: pendingTotal });
      } else {
        res.status(200);
      }
    })
    .catch(err => {
      res.status(500).json({ message: "Something went wrong" });
    });
};

Upvotes: 1

Views: 203

Answers (1)

adrihanu
adrihanu

Reputation: 1432

How about returning an empty string if the total is not greater than zero?

res.status(200).json({ html: '' });

Upvotes: 1

Related Questions