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.