Please help with stock price checker

Hello,
Can you tell me where is the mistake?

'use strict';

const fetch = require('node-fetch');

// our inner DB

const stocksDB = [];

async function getStock(stock) {
  const fetchResponse = await fetch(
    `https://repeated-aplace.glitch.me/v1/stock/${stock}/quote`
  );
  const { symbol, latestPrice } = await fetchResponse.json();

  return {
    symbol,
    price: `${latestPrice}`,
  };
}

async function addToStocksDB(stock, like) {
  const returnedStock = await getStock(stock);
  const { symbol, price } = returnedStock;

  if (symbol) {
    if (symbolsDB.hasOwnProperty(symbol)) {
      symbolsDB[symbol] = {
        price,
        like: like ? symbolsDB[symbol].like + 1 : symbolsDB[symbol].like,
      };
    } else {
      symbolsDB[symbol] = {
        price,
        like: like ? 1 : 0,
      };
    }
  }
}

module.exports = function (app) {
  app.route('/api/stock-prices').get(async function (req, res) {
    const { stock, like } = req.query;

    const stockKeys = [];

    if (stock) {
      if (typeof stock === 'string') {
        await addToStocksDB(stock, like);

        stockKeys.push(stock.toUpperCase());
      } else {
        // is an array
        for (let i = 0; i < stock.length; i++) {
          await addToStocksDB(stock[i], like);

          stockKeys.push(stock[i].toUpperCase());
        }
      }
    }

    const result =
      stockKeys.length === 1
        ? { ...stocksDB[stockKeys[0]], stock: stockKeys[0] }
        : object
            .keys(stocksDB)
            .filter((key) => stockKeys.includes(key))
            .map((key) => ({
              stock: key,
              ...stocksDB[key],
            }));

    res.json({
      stockData: result,
    });
  });
};

//{"stockData":{"stock":"GOOG","price":"786.90","likes":1}}
//{"stockData":[{"stock":"MSFT","price":"62.30","rel_likes":-1},{"stock":"GOOG","price":"786.90","rel_likes":1}]}

I don’t know what lesson this is. Please use the ask for help button which will generate the challenge link and code.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Stock price checker from information security

Oh ok cool.
I haven’t done the information security section yet so I won’t be able to help you.

You will need to still use the ask for help button so someone who has worked on the project can assist you.

Good luck!

I edited the second code block because it looks like a markdown link snug in your fetch call. I also formatted it using prettier.

What is the link you are using for the Price Checker API? It isn’t the one from the challenge page.

Stock Price Checker Proxy

Usage:

GET https://stock-price-checker-proxy.freecodecamp.rocks/v1/stock/[symbol]/quote

Where:

symbol = msft | goog | aapl | …

Edit: what is symbolsDB supposed to be?

When asking for help with the projects it’s better to link to the version on replit (or wherever you have it).

It come this when I enter the link of install and set up mongoose

“mongoose” should be connected to a database

I have created a new post since this was off topic from the last topic.

I would suggest sharing your replit link so people can take a look at your full project.

As mentioned earlier, I have not worked on this project so I am not going to be able to assist you.

But @lasjorg has taken the time to answer your post.
Have you looked at anything that he has mentioned to you?

Hello,

This is the link of Install and Set Up Mongoose of replit

https://boilerplate-mongomongoose-4.memojojo.repl.co
When I enter the link it comes this “mongoose” should be connected to a database

We currently can’t see the code.

But it looks like you are talking about this challenge.

What happened to your question about the stock price checker?
Did you resolve the issues you were having here?

'use strict';

const fetch = require('node-fetch');

// our inner DB

const stocksDB = [];

async function getStock(stock) {
  const fetchResponse = await fetch(
    `https://repeated-aplace.glitch.me/v1/stock/${stock}/quote`
  );
  const { symbol, latestPrice } = await fetchResponse.json();

  return {
    symbol,
    price: `${latestPrice}`,
  };
}

async function addToStocksDB(stock, like) {
  const returnedStock = await getStock(stock);
  const { symbol, price } = returnedStock;

  if (symbol) {
    if (symbolsDB.hasOwnProperty(symbol)) {
      symbolsDB[symbol] = {
        price,
        like: like ? symbolsDB[symbol].like + 1 : symbolsDB[symbol].like,
      };
    } else {
      symbolsDB[symbol] = {
        price,
        like: like ? 1 : 0,
      };
    }
  }
}

module.exports = function (app) {
  app.route('/api/stock-prices').get(async function (req, res) {
    const { stock, like } = req.query;

    const stockKeys = [];

    if (stock) {
      if (typeof stock === 'string') {
        await addToStocksDB(stock, like);

        stockKeys.push(stock.toUpperCase());
      } else {
        // is an array
        for (let i = 0; i < stock.length; i++) {
          await addToStocksDB(stock[i], like);

          stockKeys.push(stock[i].toUpperCase());
        }
      }
    }

    const result =
      stockKeys.length === 1
        ? { ...stocksDB[stockKeys[0]], stock: stockKeys[0] }
        : object
            .keys(stocksDB)
            .filter((key) => stockKeys.includes(key))
            .map((key) => ({
              stock: key,
              ...stocksDB[key],
            }));

    res.json({
      stockData: result,
    });
  });
};

//{"stockData":{"stock":"GOOG","price":"786.90","likes":1}}
//{"stockData":[{"stock":"MSFT","price":"62.30","rel_likes":-1},{"stock":"GOOG","price":"786.90","rel_likes":1}]}

If so, then you need to let us know and we can mark this as resolved.

If you are talking about the set up mongoose challenge then you should reply back to that topic here.

But mixing two different challenges in the same topic makes things very confusing.
make sense?

Edit: I will move my reply to the other thread.

For the Stock Price Checker challenge what I said before still stands.

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