Help with async/await Stock Price Checker EDIT: Still need help

EDIT: I still need help please see my latest comment below.
Full code still here.

What I want to do is get a stock price from an API and then save that price in my database.

My request to the API functions properly and retrieves the stock price. However, (I think) my function to update the database executes before the stock price is retrieved so in my database is price: null.

I’ve been reading up on async /await to solve this but haven’t figured out how to use it properly.

var Stock = mongoose.model("Stock", stockSchema);

  app.route('/api/stock-prices')
    .get(function (req, res){
    var stock1 = req.query.stock1; 
    stock1.toUpperCase();
    var stock2 = req.query.stock2; //if stock2 compare stock prices
    if (stock2) { stock2.toUpperCase();}
    var like = req.query.like ? 1 : 0;
    //console.log("like " + like)
    var ip = like ? req.ip : null;
    //console.log("ip is " + ip);
    //var stockPrice;    
    
    var getStockPrice = async (stock) => {  
      var url = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol="
                + stock + "&apikey=" + process.env.ALPHA_API_KEY;
      request(url, {json: true}, function(err, res, body) {
        if (err) { return console.log(err); }
        else {
          console.log("stockPrice = " + body["Global Quote"]["05. price"]); //correctly logs stock price
          return body["Global Quote"]["05. price"];
          
        } 
      })
    };
   
    var addNewStock = async (stock) => {
      var stockPrice = await getStockPrice(stock);
      var newStock = await new Stock({stock: stock, price: stockPrice, likes: like});
      console.log(newStock);
      newStock.save( (err, doc) => {
        if (err) { console.log(err); }
        else {
          res.json({"stock": doc.stock, "price": doc.price, "likes": doc.likes})
        }
      });
    };
    
    var updateStockPriceAndLikes = async (stock) => {
      var stockPrice = await getStockPrice(stock);
      await Stock.findOneAndUpdate({stock: stock}, {price: stockPrice, $inc: {likes: like}, $push: {ip: ip}},
                             {new: true}, function(err, doc) {
        if (err) { console.log(err); }
        else if (!doc) { console.log("updateStockPriceAndLikes failed"); }
        else { console.log("updateStockPriceAndLikes was a success"); }
      })
    };
    
    var updateStockPrice = async (stock) => {
      var stockPrice = await getStockPrice(stock);
      await Stock.findOneAndUpdate({stock: stock}, {price: stockPrice},
                             {new: true}, function(err, doc) {
        if (err) { console.log(err); }
        else if (!doc) { console.log("updateStockPrice failed"); }
        else { console.log("updateStockPrice was a success"); }
      })
    };
    
    async function handleStock1(stock1) {
      if (stock1) {
      if (ip) {   // like is checked
        Stock.findOne({stock: stock1}, function(err, doc) {
          if (err) { console.log(err); }
          else if (!doc) {
            addNewStock(stock1); //not in db, add new
          } else if (doc.ip.indexOf(ip) < 0) {  //ip not found
            updateStockPriceAndLikes(stock1);   //and push ip to db
          } else {
            updateStockPrice(stock1);
          }
        })
      } else if (!ip) {   //like is not checked
        Stock.findOne({stock: stock1}, function(err, doc) {
          if (err) { console.log(err); }
          else if (!doc) {
            addNewStock(stock1);
          } else {
            updateStockPrice(stock1);
          }
        });
      }
    };
  }
  handleStock1(stock1); 
       
    });
    
};

Full code here.
Project here
App page, not sure if it will log anything on the code page for you. Only working on entering one stock and trying to get that to work before entering two stocks.

There are several places where you’ve declared your functions to be async, but you haven’t implemented await. Wherever you want your code to stop executing until you, say, get a response from an internal function, you use await to keep subsequent code from executing until the await block is resolved.

This is a pretty good introduction to async/await.

I read the article, it doesn’t seem like it should be this hard. I’ve tried

async function handleStock1(stock1) {
      await getStockPrice(stock1);
      if (stock1) {
       if (ip) {   // like is checked
        Stock.findOne({stock: stock1}, function(err, doc) {
          if (err) { console.log(err); }
          else if (!doc) { 
            addNewStock(stock1); //not in db, add new
          } else if (doc.ip.indexOf(ip) < 0) {  //ip not found
            updateStockPriceAndLikes(stock1);   //and push ip to db
          } else {
            updateStockPrice(stock1);
          }
        })

and

function handleStock1(stock1) {
      if (stock1) {
       if (ip) {   // like is checked
        Stock.findOne({stock: stock1}, async function(err, doc) {
          if (err) { console.log(err); }
          else if (!doc) { 
            await getStockPrice(stock1);
            addNewStock(stock1); //not in db, add new
          } else if (doc.ip.indexOf(ip) < 0) {  //ip not found
            await getStockPrice(stock1);
            updateStockPriceAndLikes(stock1);   //and push ip to db
          } else {
            await getStockPrice(stock1);
            updateStockPrice(stock1);
          }
        })

and tried getStockPrice like this

var addNewStock = async (stock) => {
      await getStockPrice(stock);
      var newStock = new Stock({stock: stock, price: stockPrice, likes: like});
      console.log(newStock);
      newStock.save( (err, doc) => {
        if (err) { console.log(err); }
        else {
          res.json({"stock": doc.stock, "price": doc.price, "likes": doc.likes})
        }
      });
    };

I think I’ve tried another 200 combinations and price is null in the database if updating a price, and price is not added to the database at all when a new stock is added.

It took me all day but I got this figured out by refactoring the order of my code. I didn’t use async/await or promises (or callback hell). Now on to the next step of this project. I fear I may have to eventually use async/await and promises on this project but I guess that is the learning process.

Back to struggling with async / await, or I could use promises but can’t get that to work either.
This

getStockPrice(stock1);
if (stock2) {getStockPrice(stock2)}

Will give the result I want:

stockPrice = 1250.4100  //goog
updateStockPrice was a success
[ { stock: 'GOOG', price: '1250.4100', likes: 0 }]  //this is responseStock variable
stockPrice = 141.3400  // msft
updateStockPrice was a success
[ { stock: 'GOOG', price: '1250.4100', likes: 0 },
  { stock: 'MSFT', price: '141.3400', likes: 0 } ]  //now responseStock variable

But if I check stocks too often I get the following and I have to wait a while for it to work again

stockPrice = 1250.4100
stockPrice = 1250.4100
updateStockPrice was a success
[ { stock: 'GOOG', price: '1250.4100', likes: 0 } ]
updateStockPrice was a success
[ { stock: 'GOOG', price: '1250.4100', likes: 0 },
  { stock: 'MSFT', price: '1250.4100', likes: 0 } ]

I’m not sure why doing something like the following does not stop execution of the code until the awaited function resolves

async function begin() {
      await getStockPrice(stock1); //which calls handleStock, which calls updateStockPrice
      if (stock2) {await getStockPrice(stock2)};
      sendResponse(responseStock);
    };
    
begin();

This sends a response of {} instead of it should be similar to [ { stock: 'GOOG', price: '1250.4100', likes: 0 }, { stock: 'MSFT', price: '1250.4100', likes: 0 } ]

So the sendResponse function executes before the data is pushed into var responseStock by the two awaited functions.