传递数据时变得不确定(Typescript-量角器)

时间:2019-03-11 03:19:05

标签: typescript protractor papaparse

当直接调用enterUsingCSV()时,我得到了不确定的值。在GetDataExcel()中调用该函数时,该函数正确运行。谁能解释发生了什么事?

//// 
GetDataExcel(col: number) {
        this.papa.parse(this.file, {
            complete: async (result: any) => {
                let cc = result.data[1][col]
                //console.log(cc)
                return cc
            }
        })
    }

///
 enterUsingCSV(column:number){
    let value = this.GetDataExcel(column)
    console.log(value)
    // this.enterText("username", "id", value)

  }

//// 
e.enterUsingCSV(2);

1 个答案:

答案 0 :(得分:1)

根据文档,当我们使用本地文件调用Papa.parse时,parse方法不会返回任何内容。而是将结果异步提供给回调函数。这正是您所看到的行为。

GetDataExcel(col: number) {
  // The result value will always be undefined here,
  // because when we pass a local file to parse,
  // parse does not return anything.
  const result = this.papa.parse(this.file, {
    complete: async (result: any) => {
      // Instead, the results are passed to this complete callback.
      let cc = result.data[1][col];
      console.log(cc);
      // There is no point in returning cc here, 
      // because there is nothing waiting to receive the return value.
      return cc;
    }
  });
}

enterUsingCSV(column:number) {
  // The value here will also be undefined. There are two reasons for that. 
  // First, the GetDataExcel method isn't returning anything. Second, even if 
  // it did return the result constant, the result constant is always undefined, 
  // because Papa.parse does not return anything when we pass it a local file. 
  let value = this.GetDataExcel(column);
  console.log(value);
}

e.enterUsingCSV(2);

以下是相关文档:

Parse local files

Papa.parse(file, config)
  

file是从DOM获得的File对象。

     

config是一个包含回调的配置对象。

     

不返回任何内容。结果异步提供给回调函数。