Tell us what’s happening:
The test is showing All 5 functional tests are complete and passing. However, the 6th test that All 5 functional tests are complete and passing fails. I test the functions and they works. Can someone please help me with it? thanks.
Here is the link for my repl: https://boilerplate-project-stockchecker-2.chloechloew.repl.co
Your code so far
'use strict';
var expect = require('chai').expect;
let mongodb = require('mongodb')
let mongoose = require('mongoose')
let XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest
module.exports = function (app) {
let uri = 'mongodb+srv://chloe:' + process.env.PW +'@cluster0.mjwcd.mongodb.net/stock?retryWrites=true&w=majority'
mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
let stockSchema = new mongoose.Schema({
name: {type: String, required: true},
likes: {type: Number, default: 0}, ips: [String]
})
let Stock = mongoose.model('Stock', stockSchema)
app.route('/api/stock-prices')
.get(function (req, res){
let responseObject = {}
responseObject['stockData'] = {}
let twoStocks = false
let findOrUpdateStock = (stockName, documentUpdate, nextStep) => {
Stock.findOneAndUpdate(
{name: stockName},
documentUpdate,
{new: true, upsert: true},
(error, stockDocument) => {
if(error){
console.log(error)
}else if(!error && stockDocument){
if(twoStocks === false){
return nextStep(stockDocument, processOneStock)
}else{
return nextStep(stockDocument, processTwoStocks)
}
}
}
)
}
let likeStock = (stockName, nextStep) => {
Stock.findOne({name: stockName}, (error, stockDocument) => {
if(!error && stockDocument && stockDocument['ips'] && stockDocument['ips'].includes(req.ip)){
return res.json('Error: Only 1 Like per IP Allowed')
}else{
let documentUpdate = {$inc: {likes: 1}, $push: {ips: req.ip}}
nextStep(stockName, documentUpdate, getPrice)
}
})
}
let getPrice = (stockDocument, nextStep) => {
let xhr = new XMLHttpRequest()
let requestUrl = 'https://stock-price-checker-proxy--freecodecamp.repl.co/v1/stock/' + stockDocument['name'] + '/quote'
xhr.open('GET', requestUrl, true)
xhr.onload = () => {
let apiResponse = JSON.parse(xhr.responseText)
stockDocument['price'] = Number(apiResponse['latestPrice'].toFixed(2))
nextStep(stockDocument, outputResponse)
}
xhr.send()
}
let processOneStock = (stockDocument, nextStep) => {
responseObject['stockData']['stock'] = stockDocument['name']
responseObject['stockData']['price'] = Number(stockDocument['price'])
responseObject['stockData']['likes'] = stockDocument['likes']
nextStep()
}
let stocks = []
let processTwoStocks = (stockDocument, nextStep) => {
let newStock = {}
newStock['stock'] = stockDocument['name']
newStock['price'] = Number(stockDocument['price'])
newStock['likes'] = stockDocument['likes']
stocks.push(newStock)
if(stocks.length === 2){
stocks[0]['rel_likes'] = stocks[0]['likes'] - stocks[1]['likes']
stocks[1]['rel_likes'] = stocks[1]['likes'] - stocks[0]['likes']
responseObject['stockData'] = stocks
nextStep()
}else{
return
}
}
/* Process Input*/
if(typeof (req.query.stock) === 'string'){
/* One Stock */
let stockName = req.query.stock
let documentUpdate = {}
if(req.query.like && req.query.like === 'true'){
likeStock(stockName, findOrUpdateStock)
}else{
findOrUpdateStock(stockName, documentUpdate, getPrice)
}
} else if (Array.isArray(req.query.stock)){
twoStocks = true
let stockName = req.query.stock[0]
if(req.query.like && req.query.like === 'true'){
likeStock(stockName, findOrUpdateStock)
}else{
let documentUpdate = {}
findOrUpdateStock(stockName, documentUpdate, getPrice)
}
stockName = req.query.stock[1]
if(req.query.like && req.query.like === 'true'){
likeStock(stockName, findOrUpdateStock)
}else{
let documentUpdate = {}
findOrUpdateStock(stockName, documentUpdate, getPrice)
}
}
});
};
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36
.
Challenge: Stock Price Checker
Link to the challenge: