Ip always added to database even if !like

Tell us what’s happening:
Describe your issue in detail here.

If I do not check the like box console.log(like) just before the ternary expression evaluates like to false, yet ip is always added to the database. Only if I replace [ip] by , it will not. I don’t see what is wrong with the ternary expression. And I don’ see the error could be somewhere else.

Your code so far

async function createStock(stock, like, ip) {
  const newStock = new StockModel({
    symbol: stock,
    likes: like ? [ip] : [],
  });
  const savedNew = newStock.save();
  return savedNew;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Stock Price Checker

Link to the challenge:

For some reason all functional tests pass, even though likes are incorrectly shown in browser. I would really like to fix the issue, because I can’t see anything wrong with the ternary statement.

As for the fcc test on security. It fails. I read that there have been many posts on this topic and would appreciate if anyone could enlighten me on how to pass that test.

I can’t tell without the rest of your code, but are you sure like is a boolean or is it a string ("false")? Because "false" is a truthy value.

@bengitter,
Sorry, this is the link to the code.
You might be right, that it is a string. Well spotted! I am trying to fix it, but am failing miserably. Could you help me?

Would you also have an idea how to pass the security test?

The easiest way to change a string to a boolean is to simply check if like === "true":

app.route('/api/stock-prices')
    .get(async function (req, res) {
      let { stock, like } = req.query;
      like = (like === "true");
      ...

I cloned your project and with this change all tests are passing for me.

1 Like

Super, thank you very much!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.