如果我将以下代码放入一个可观察到的HQ单元中,我将得到
data5 = TypeError: reading.map不是函数
data5 = {
const reading = FileAttachment("climate_graphs - china.csv").csv();
reading.map(function(element) {
return {"date": element["date"], "value": element["value"]}
})
}但是,如果我把它分解成两个单元,它就能工作:
// cell 1
reading = FileAttachment("climate_graphs - china.csv").csv();
// cell 2
data = reading.map(function(element) {
return {"date": element["date"], "value": element["value"]}
})发布于 2021-08-14 05:21:51
在FileAttachment文档中,它调用.csv()是FileAttachment的异步方法。
调用FileAttachment不会立即加载文件--只有在请求时才加载内容。它有点像Fetch API:有一些异步方法以不同的形式返回文件的内容,例如JSON、CSV或ArrayBuffer。根据要使用文件的方式选择适当的方法。
但是,对于这个问题来说,更清楚的是,在后一种情况下,您有一个单元格来读取文件/1单元来处理数据;文件是用隐式async/ await读取并分配给await的。在有错误的情况下,需要在块中指定await。
因此,这应该是可行的:
data5 = {
// use await below
const reading = await FileAttachment("climate_graphs - china.csv").csv();
// 'reading' is now an array of objects so can use 'map'
let mapping = reading.map(function(element) {
return {"date": element["date"], "value": element["value"]}
});
return mapping;
}通常,承诺导论文档中提到的单元格具有这种特定的行为。
你看到承诺了吗?可观察到的隐式等待承诺跨越单元边界,因此您通常不需要直接处理承诺。单元格可以返回承诺,而其他单元格可以简单地引用值,它们将在承诺解析时运行。
https://stackoverflow.com/questions/68779573
复制相似问题