我有一些代码,试图在其中绘制库存数据图表。值已成功获取,但尝试绘制图表时出现错误。
专门查看图表功能。当我使用console.log(hist);和console.log(q)我能够从它们获取相应的值。
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")}`);
};
但是当我使用console.log(chart);时我最终遇到此错误:
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)
答案 0 :(得分:1)
该错误发生在asciichart.plot(x, { height: height })
,因此数组长度错误表示x
不是正确的数组。
如果您发布的代码是完整代码,则您的x
似乎未定义,这将导致无效的数组长度错误,因为asciichart.plot要求第一个参数为数组