Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
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);

        });

      }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Looks like data is either undefined or dataReturned is returning 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 == "" ?
about 4 years ago · Juan Pablo Isaza Report

0

Create an if condition as below

    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. If null/undefined/false, it will not execute whatever provided in the if condition.

Why the error happened?
You are trying to get something from a null/undefined. It is something like null.slice() you did

about 4 years ago · Juan Pablo Isaza Report

0

You can also try something like:

google.script.run.withSuccessHandler(function(dataReturned){
      data = dataReturned?.slice();
    }).getDataForSearch();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!