Этот код является рабочим решением для Web Api Controller.Проблемы начинаются, когда я пытаюсь реализовать разделение проблем и импортировать fetchWebApi(url)
из const { fetchWebApi } = require('./fetchWebApiController')
.Это дает мне следующую ошибку: curValue
не является функцией.Не уверен, почему это говорит функцию.curValue
не является функцией.Это просто переменная, которая используется как переменная и никогда не вызывается как функция.Вот ссылка на проект github.
webApiController.js
const axios = require('axios')
// const { fetchWebApi } = require('./fetchWebApiController')
const { Stock } = require('../models/Stock')
exports.getWebApi = (req, res) => {
let curValue = req.params.symbol
console.log(`${curValue} - seacrhBox value`)
console.log(typeof curValue)
const urlCompact = `https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=${curValue}&outputsize=compact&apikey=6BUYSS9QR8Y9HH15`
async function fetchWebApi(url) {
try {
const response = await axios.get(url)
return parsedData = await Object.keys(response.data['Time Series (Daily)']).map(date => {
return {
date: Date.parse(date),
open: Math.round(parseFloat(response.data['Time Series (Daily)'][date]['1. open']) * 100) / 100,
high: Math.round(parseFloat(response.data['Time Series (Daily)'][date]['2. high']) * 100) / 100,
low: Math.round(parseFloat(response.data['Time Series (Daily)'][date]['3. low']) * 100) / 100,
close: Math.round(parseFloat(response.data['Time Series (Daily)'][date]['4. close']) * 100) / 100,
volume: parseInt(response.data['Time Series (Daily)'][date]['5. volume'])
}
})
} catch (ex) {
console.log(`fetchWebApi error: ${ex}`)
}
}
(async function creatStock() {//line33
try {
const webApiData = await fetchWebApi(urlCompact)
const stock = new Stock({
symbol: curValue,
data: webApiData
})
const query = { symbol: `${curValue}` }
const update = { $addToSet: { data: stock.data } }
const options = { upsert: true, new: true }
const stockResult = await Stock.findOneAndUpdate(query, update, options)
console.log('Saved the symbol web TO db', stockResult.symbol)
return res.send(webApiData)
} catch (ex) {
console.log(`creatStock error: ${ex}`)
}
})()
}
Ошибка
TypeError: curValue is not a function
at exports.getWebApi (/mnt/c/Users/john/Desktop/node/controllers/webApiController.js:33:3)
at Layer.handle [as handle_request] (/mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/layer.js:95:5)
at next (/mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/layer.js:95:5)
at /mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/index.js:281:22
at param (/mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/index.js:354:14)
at param (/mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/index.js:365:14)
at Function.process_params (/mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/index.js:410:3)
at next (/mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/index.js:174:3)
at router (/mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/index.js:317:13)
at /mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/mnt/c/Users/john/Desktop/node/node_modules/express/lib/router/index.js:335:12)