javascript - 无法显示标记。setMarkers() 函数返回 TypeError: t.map is not a function
问题描述
我查看了 github 上的文档,但找不到更多关于标记的信息。这是他们给出的示例: https ://github.com/tradingview/lightweight-charts/blob/ef8cfa40cb51ee1f9a5c11bd099bc510c022b010/docs/series-basics.md#setmarkers
我似乎有正确的标记数组,但没有运气。
async function getCandle() {
while(true){
await fetch('localhost:5000/candle.json')
.then(res => res.text())
.then(data => {
/* Handling of data */
candleSeries.setMarkers(getMarkers()); // returns TypeError: t.map is not a function at i.t.setMarkers
// chart.setMarkers(getMarkers()); returns TypeError: chart.setMarkers is not a function
})
await sleep(1000);
}
}
async function getMarkers(){
await fetch('http://localhost:5000/markers.jsonl')
/* markers.jsonl looks like this:
{"time": 1592913600, "position": "belowBar", "shape": "arrowUp", "color": "green", "id": 1, "text": "BUY"}
{"time": 1592913900, "position": "belowBar", "shape": "arrowUp", "color": "green", "id": 1, "text": "BUY"}
*/
.then(res => res.text())
.then(data => {
data = data.split("\n");
let markers = data.map(d => {
// Parse d from string to JSON
d = JSON.parse(d);
return {time: d["time"], position: d["position"], shape: d["shape"], color: d["color"], id: d["id"], text: d["text"]}
});
return markers;
})
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
解决方案
getMarkers
是异步函数,Promise
如果你不这样做,它会返回一个实例await
。
您需要将数据处理程序标记为async
函数和await getMarkers
结果:
async function getCandle() {
while(true){
await fetch('localhost:5000/candle.json')
.then(res => res.text())
.then(async (data) => {
/* Handling of data */
candleSeries.setMarkers(await getMarkers());
})
await sleep(1000);
}
}
编辑(来自@Nipheris 评论):你没有从函数返回任何东西getMarkers
,所以你需要在return
那里添加语句。
推荐阅读
- angular - 模块“AppModule”声明的意外值“日历”。请添加@Pipe/@Directive/@Component 注解
- vue.js - 我无法在组件中映射我的 vuex 操作
- variables - VHDL 中的变量使用
- python - 到 Panda 数据框中下一个间隙(日期时间列)的距离
- sql - 如何计算 PostgreSQL 中的布尔变化
- java - 如何在 Apache Commons 配置中使用 XPath 按值查询 XMLConfiguration?
- javascript - 如何在 React 中预取组件以供以后使用?
- html - HTML 中的 Google 字体
- three.js - 有没有办法在不影响 RGB 值的情况下修改纹理中的 alpha?
- r - 如何用 sd 并排制作发散条形图