【问题标题】:data.indexOf is not a function error in [tabulator]data.indexOf 不是 [tabulator] 中的函数错误
【发布时间】:2023-01-05 22:39:57
【问题描述】:

data.indexOf 不是 Tabulator JavaScript 库中的函数错误。

完整的错误信息:

[Error] TypeError: data.indexOf is not a function. (In 'data.indexOf("{")', 'data.indexOf' is undefined)
    load (tabulator.js:6075)
    _loadInitialData (tabulator.js:7971)
    _create (tabulator.js:7841)
    (anonieme functie) (tabulator.js:7756)

并警告:

Table Not Initialized - Calling the redraw function before the table is initialized may result in inconsistent behavior, Please wait for the `tableBuilt` event before calling this function

我使用了文档中描述的设置,基于浏览器的 tabulator.js 和 tabulator.css 在 html 中的位置。其他什么都没做。

我的 HTML:

<!DOCTYPE html>
<html lang="nl">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="css/tabulator.css">
  <!-- <link rel="stylesheet" href="css/tabulator.css.map"> -->
  <link rel="stylesheet" href="css/layout.css">
  <link rel="stylesheet" href="css/style.css">
  <script defer src="js/tabulator.min.js"></script>
  <script defer src="js/crud.js"></script>
  <script defer src="js/data.js"></script>
  <script defer src="js/assess.js"></script>
  <title>DMMA</title>
</head>
<body>
  <main id="home">
    <h1 class="center-text dark-blue-text">Datamanagement Maturity Assessment</h1>

    <div id="entree" class="container"></div>
    <div class="output container"></div>
  </main>
</body>
</html>

我在 assess.js 中的 javascript:

document.addEventListener('DOMContentLoaded', async function() {
  
  // the assObj is the data of assessment objects I want to retrieve
  const assObj = new dataProvider;
 
  // Use Tabulator
  assObj.get('api.php/records/AssmntPlusObjects')

    // .then(async dataArray => displayRecords(dta, false))   // Works!
    .then(dataArray => {
      let table = new Tabulator("#entree", {
        data: dataArray,
        autoColumns: true
      })
    })
    .catch(err => displayError(err, null))
});

assObj.get 转到一个获取类,该类从 MySQL 数据库获取数据,该数据库通过 PHP 通用 API 获取数据。一切正常。 带有对象的数据数组转换为 javascript 对象 OK。制表符给出了上述错误。

该站点位于 Internet 提供商主机上,我不想在本地运行另一个 MySQL。

有什么建议么?设置错误?

【问题讨论】:

  • 在构造 Tabulator 实例之前,dataArray 的值是多少?
  • 调试并找出 dataArray 是什么。 console.log(typeof dataArray, dataArray);

标签: javascript tabulator


【解决方案1】:

dataArray 为空或未定义。通过console.log(dataArray)查看

【讨论】:

    【解决方案2】:

    data.indexOf is not a function in [tabulator] 错误通常发生在数据变量不是数组或没有 indexOf 方法时。

    indexOf 方法是 Array 原型的内置方法,它返回可以在数组中找到给定元素的第一个索引,如果不存在则返回 -1。它是这样使用的:

    const fruits = ['apple', 'banana', 'mango'];
    const index = fruits.indexOf('banana'); // returns 1

    如果数据变量不是数组或没有 indexOf 方法,则调用 data.indexOf() 将导致错误。

    要修复此错误,您需要确保数据变量是数组或具有 indexOf 方法的对象。如果不是,则需要将其转换为数组或寻找替代方法来执行您尝试执行的操作。

    例如,如果数据是一个对象,您可以使用 Object.values 方法提取对象值的数组,然后对结果数组使用 indexOf 方法:

    const data = { a: 1, b: 2, c: 3 };
    const values = Object.values(data); // [1, 2, 3]
    const index = values.indexOf(2); // returns 1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2017-10-23
      • 2013-01-04
      • 1970-01-01
      相关资源
      最近更新 更多