gpt4 book ai didi

javascript - 范围错误: Invalid array length NodeJS

转载 作者:行者123 更新时间:2023-12-02 22:50:34 25 4
gpt4 key购买 nike

我有一些代码,我正在尝试绘制股票数据图表。这些值已成功提取,但在尝试绘制图表时出现错误。

具体查看图表函数。当我使用 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)

最佳答案

错误发生在 asciichart.plot(x, { height: height }) 处,因此数组长度错误意味着 x 不是一个正确的数组。

如果您发布的代码是完整代码,则您的 x 似乎未定义,这将导致无效的数组长度错误,因为 asciichart.plot 要求第一个参数为数组

关于javascript - 范围错误: Invalid array length NodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58193994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com