You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React集成WebDataRocks:捕获数据透视表更新报错排查

解决React集成WebDataRocks时getData()调用报错的问题

问题核心

你调用getData()的方式有误——WebDataRocks的getData()是异步方法,必须传入回调函数来接收返回数据,不能直接同步获取返回值。你的代码里直接调用this.myRef.webdatarocks.getData(),未传递回调参数,导致内部逻辑执行失败,抛出TypeError: s is not a function(此处s就是预期的回调参数)。

修复方案

1. 修正getData()调用逻辑

将reportComplete方法改为通过回调函数获取数据:

reportComplete = () => {
  // 正确调用getData,传入回调处理返回结果
  this.myRef.current.webdatarocks.getData((data) => {
    console.log(">>>>>> 修改后的数据:", data);
  });
  
  // getReport()为同步方法,可直接调用
  console.log(">>>>>> 当前报表数据源:", this.myRef.current.webdatarocks.getReport().dataSource);
};

2. 统一Ref使用方式(可选但推荐)

你代码中混合了createRef和回调ref,可统一为createRef的标准用法,避免潜在问题:

// 初始化ref
myRef = React.createRef();

// render中绑定ref
<WebDataRocksReact.Pivot
  ref={this.myRef}
  // 其他属性保持不变
/>

完整修复后的代码

import React from "react";
import * as WebDataRocksReact from "@webdatarocks/react-webdatarocks";

export class App extends React.Component {
  myRef = React.createRef();

  jsonData = [
    { Country: "United States", Population: 331002651 },
    { Country: "China", Population: 1444216107 },
    { Country: "India", Population: 1380004385 },
    { Country: "Indonesia", Population: 273523615 },
    { Country: "Pakistan", Population: 220892340 },
    { Country: "Brazil", Population: 212559417 },
    { Country: "Nigeria", Population: 206139589 },
    { Country: "Bangladesh", Population: 164689383 },
    { Country: "Russia", Population: 145934462 },
    { Country: "Mexico", Population: 128932753 },
    { Country: "Japan", Population: 126476461 },
    { Country: "Ethiopia", Population: 114963588 },
    { Country: "Philippines", Population: 109581078 },
    { Country: "Egypt", Population: 102334404 },
    { Country: "Vietnam", Population: 97338579 },
  ];

  myCustomReport = {
    dataSource: { data: this.jsonData },
    slice: {
      rows: [{ uniqueName: "Country" }],
      columns: [{ uniqueName: "Measures" }],
      measures: [{ uniqueName: "Population", aggregation: "sum" }],
    },
  };

  reportComplete = () => {
    this.myRef.current.webdatarocks.getData((data) => {
      console.log(">>>>>> 修改后的数据:", data);
    });
    console.log(">>>>>> 当前报表数据源:", this.myRef.current.webdatarocks.getReport().dataSource);
  };

  render() {
    return (
      <div>
        <WebDataRocksReact.Pivot
          ref={this.myRef}
          toolbar={true}
          report={this.myCustomReport}
          reportchange={() => this.reportComplete()}
        />
      </div>
    );
  }
}

export default App;

额外说明

  • reportchange事件会在用户修改透视表(如拖拽字段、调整聚合方式)时触发,适合捕获用户操作。
  • 所有WebDataRocks的异步API(如getData())都需要通过回调函数处理返回结果,这是库的标准设计逻辑。

内容的提问来源于stack exchange,提问作者Christina Michailidou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 12:26:04