简体   繁体   English

未捕获的类型错误:无法读取 null 的属性(正在读取“切片”)------ 未捕获的类型错误:无法读取未定义的属性(正在读取“过滤器”)

[英]Uncaught TypeError: Cannot read properties of null (reading 'slice') ------ Uncaught TypeError: Cannot read properties of undefined (reading 'filter')

I really appreciate some help with this code.我非常感谢此代码的一些帮助。 It was working before in google sheets but suddenly started showing up with error messages.它之前在谷歌表格中工作,但突然开始显示错误消息。

The full code is below :完整代码如下:

function getDataForSearch() {

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName("TEST FORMULAR");
  return ws.getRange(6, 1, ws.getLastRow()-1, 6).getValues();
  
}

function setDataForSearch(){

        google.script.run.withSuccessHandler(function(dataReturned){
          data = dataReturned.slice();
        }).getDataForSearch();

      }

      
      function search(){

        var searchInput = document.getElementById("searchInput").value.toString().toLowerCase().trim();
        var searchWords = searchInput.split(/\s+/);
        var searchColumns = [0];

        var resultsArray = searchInput == "" ? [] : data.filter(function(r){

          return searchWords.every(function(word){
            return searchColumns.some(function(colIndex){
              return r[colIndex].toString().toLowerCase().indexOf(word) != -1;
            });
          });
    
        });

        var searchResultsBox =  document.getElementById("searchResults");
        var templateBox = document.getElementById("rowTemplate");
        var template = templateBox.content;

        searchResultsBox.innerHTML = "";

        resultsArray.forEach(function(r){

          var tr = template.cloneNode(true);
          var typeInitiativeColumn = tr.querySelector(".type-initiative");
          var pepRefColumn = tr.querySelector(".pep-ref");
          var projectNameColumn = tr.querySelector(".project-name");

          pepRefColumn.textContent = r[0];
          typeInitiativeColumn.textContent = r[1];
          projectNameColumn.textContent = r[2];
          searchResultsBox.appendChild(tr);

        });

      }

Looks like data is either undefined or dataReturned is returning null .看起来data要么是undefined要么dataReturned正在返回null You can do a workaround, like你可以做一个解决方法,比如

var data = []; // define globally

google.script.run.withSuccessHandler(function (dataReturned) {
    if (dataReturned.length) { // check if dataReturned is not null in setDataForSearch function
        data = dataReturned.slice();
    }
}).getDataForSearch();

// then check if data is defined & not empty in search function
var resultsArray = !data.length || searchInput == "" ? [] : data.filter(function (r) {

});

Edit Replace编辑替换

!data.length || searchInput == "" ?

with

data === null || searchInput == "" ?

Create an if condition as below创建一个 if 条件如下

    google.script.run.withSuccessHandler(function(dataReturned){
      if(dataReturned && typeof dataReturned !== 'undefined'){
          data = dataReturned.slice();
      }else {
          data = "null or undefined" 
                 //or do whatever you want in else
     }
    }).getDataForSearch();

It will check if dataReturned is not null or undefined.它将检查dataReturned是否不为空或未定义。 If null/undefined/false, it will not execute whatever provided in the if condition.如果为 null/undefined/false,则不会执行 if 条件中提供的任何内容。

Why the error happened?为什么会发生错误?
You are trying to get something from a null/undefined.您正试图从 null/undefined 中获取一些东西。 It is something like null.slice() you did这就像你所做的null.slice()

You can also try something like:您还可以尝试以下操作:

google.script.run.withSuccessHandler(function(dataReturned){
      data = dataReturned?.slice();
    }).getDataForSearch();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'slice') - Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'slice') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') 未捕获的类型错误:无法读取未定义的属性(读取“”) - Uncaught TypeError: Cannot read properties of undefined (reading '') 未捕获的类型错误:无法读取未定义(读取“过滤器”)JavaScript 的属性 - Uncaught TypeError: Cannot read properties of undefined (reading 'filter') JavaScript Uncaught TypeError TypeError:无法读取未定义的属性(读取“路径”) - Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'path') 未捕获的类型错误:无法读取未定义的属性(读取“目标”)和(读取“值”) - Uncaught TypeError: Cannot read properties of undefined (reading 'target') & (reading 'value') 未捕获的类型错误:无法读取未定义的属性(读取“未定义”) - Uncaught TypeError: Cannot read properties of undefined (reading 'undefined') TypeError:无法读取 null 的属性(读取“切片”) - TypeError: Cannot read properties of null (reading 'slice')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM