Reputation: 81
I have some code, where I am trying to chart stock data. The values are being successfully pulled in but I am getting an error when trying to chart.
Looking specifically at the chart function. when I use console.log(hist); and console.log(q) I am able to get the respective values from them.
const asciichart = require("asciichart");
const ora = require("ora");
const chalk = require("chalk");
const fetch = require("node-fetch");
const { IEXCloudClient } = require("node-iex-cloud");
const { history } = require("yahoo-stocks"); //Special Thanks to: Fabian Beuke aka Madnight (https://github.com/madnight)
const { interpolateArray } = require("array-interpolatejs");
const { toHumanString } = require("human-readable-numbers");
const { identity, defaultTo, pipe } = require("lodash/fp");
const { tail, flatMap } = require("lodash/fp");
const { compact, map } = require("lodash/fp");
module.exports = args => {
let symbol = args.symbol || args.s;
const throbber = ora().start();
const width = defaultTo(14);
const height = defaultTo(80);
const range = defaultTo("5y");
const humanString = i => (i ? toHumanString(i).replace("G", "B") : null);
const iex = new IEXCloudClient(fetch, {
publishable: "pk_abcxyzdedfg"
});
const errorHandler = () => {
console.log(chalk.red("Error. Could not find symbol: " + symbol));
process.exit(1);
};
const getHist = async () => {
return await history(symbol.toUpperCase(), {
interval: "1m",
range: range
}).catch(errorHandler);
};
const getQuote = async i => {
return await iex
.symbols(i)
.batch("quote")
.catch(errorHandler);
};
const chart = async () => {
try {
const [hist, qt] = await Promise.all([getHist(), getQuote(symbol)]);
const chart = pipe(
map(identity),
tail,
flatMap(map("close")),
interpolateArray(width),
compact,
x => asciichart.plot(x, { height: height })
)(hist);
console.log(hist);
const q = map("quote")(qt)[0];
console.log(q);
console.log(chart);
console.log(q.companyName)
console.log(
" ".repeat(15) +
(q.companyName +
" " +
" chart. Latest Price: $" +
q.latestPrice +
" | MktCap: " +
humanString(q.marketCap))
);
} catch (error) {
console.error(error);
}
};
chart();
throbber.succeed(`${chalk.green("All Done")}`);
};
but when I use console.log(chart); I end up with this error:
RangeError: Invalid array length
at Object.exports.plot (C:\Users\Saksham\Projects\stock-cli\node_modules\asciichart\asciichart.js:29:22)
at x (C:\Users\Saksham\Projects\stock-cli\cmds\chart.js:54:25)
at C:\Users\Saksham\Projects\stock-cli\node_modules\lodash\lodash.min.js:49:144
at chart (C:\Users\Saksham\Projects\stock-cli\cmds\chart.js:55:8)
at process._tickCallback (internal/process/next_tick.js:68:7)
Upvotes: 0
Views: 2392
Reputation: 161
The error occured at asciichart.plot(x, { height: height })
, so the array length error implies that x
is not a proper array.
If the code you posted is your full code, it appears that your x
is undefined, which would cause the Invalid array length error as asciichart.plot requires first parameter to be an array
Upvotes: 1