Javascript Can';不显示标记。setMarkers()函数返回TypeError:t.map不是函数
我查看了github上的文档,但找不到更多关于markers的内容。以下是他们给出的示例: 我似乎有正确的标记数组,但没有运气Javascript Can';不显示标记。setMarkers()函数返回TypeError:t.map不是函数,javascript,markers,tradingview-api,lightweight-charts,Javascript,Markers,Tradingview Api,Lightweight Charts,我查看了github上的文档,但找不到更多关于markers的内容。以下是他们给出的示例: 我似乎有正确的标记数组,但没有运气 async function getCandle() { while(true){ await fetch('localhost:5000/candle.json') .then(res => res.text()) .then(data => { /* Handling of
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
实例
您需要将数据处理程序标记为async
函数和等待getMarkers
结果:
异步函数getCandle(){
while(true){
等待获取('localhost:5000/candle.json')
。然后(res=>res.text())
。然后(异步(数据)=>{
/*数据处理*/
setMarkers(wait getMarkers());
})
等待睡眠(1000);
}
}
编辑(来自@Nipheris comment):您从
getMarkers
函数中不返回任何内容,因此需要在此处添加return
语句。感谢您的回答。然而,我就是这么做的,现在我得到了以下结果:TypeError:无法读取I.t.上未定义的属性“map”。setMarkers@Zamo你能简化你的代码并分享它吗?我的意思是,这样每个人都可以在本地运行它并帮助你。例如,在jsfiddle@Zamo上,您的getMarkers
函数没有返回任何内容,因此setMarkers
函数从getMarkers
接收未定义的。也许把它改成这样<代码>异步函数getMarkers(){const response=wait fetch('http://localhost:8000/markers.jsonl“);const responseText=wait response.text();return responseText.split(“\n”).map(JSON.parse);}
@Nipheris我以为我在getMarkers()中返回了标记,但我想我错了。我不知道你可以像以前那样写它(但我的辩护是,我只用JS写了5天)。谢谢,现在可以用了。如何将您的评论设置为解决方案?我是新来的,所以