Uncaught Typeerror: Cannot read Property 'match' of Undefined

Asked

Viewed 570 times

0

I have a problem with this piece of code and I can’t solve it at all, every time I run the code and do the search (Auto Complete of Words) it returns the error Uncaught Typeerror: Cannot read Property 'match' of Undefined.

I already checked here and he’s calling the json, doing the mapping right, the problem is even in this macth, someone could help me with this, please?

inserir a descrição da imagem aqui

Thank you!

Code with error below:

function findMatches(wordToMatch, brands) {
  return brands.filter(place => {

    const regex = new RegExp(wordToMatch, 'gi');
    return place.codebrand.match(regex) || place.labelbrand.match(regex)
  });
}


function displayMatches() {
  const matchArray = findMatches(this.value, brands);
  const html = matchArray.map(place => {
    const regex = new RegExp(this.value, 'gi');
    const codebrandName = place.codebrand.replace(regex, `<span class="hl">${this.value}</span>`);
    const labelbrandName = place.labelbrand.replace(regex, `<span class="hl">${this.value}</span>`);
    return `
      <li>
        <span class="name"> Código: "${codebrandName}", Marca: ${labelbrandName}</span>

      </li>
    `;
  }).join('');
  suggestions.innerHTML = html;
}

or inserir a descrição da imagem aqui

  • What would that be this.value? I don’t think it refers to anything.

  • 2

    return place.codebrand.match - codebrand will not have the property match defined. This is easy to confirm by doing before this line console.log(place.codebrand);

  • @dvd I took this code from another project and made a new adaptation, practically I only changed the values of json and match information. From what I saw here the real problem is with this match, only I do not know how to solve :(

  • @Isac I didn’t quite understand your answer, could you tell me which way to solve this? Thanks

  • Surely I can’t tell because I don’t know where that comes from place.codebrand or how it was built. Better try to reproduce a functional example in the question, or show how those parts of the code came about.

  • It comes from a json, example: { "codebrand": "6927", "labelbrand": """marca2"" }, { "codebrand": "6921", "labelbrand": "marca2" },

  • Put a console.log(place.codebrand); at the beginning of the function function findMatches(wordToMatch, brands) { and see what it shows.

  • 1

    In this example of json you gave, codebrand is a number in the form of string and has no property match.

  • It displays the following error > console.log(place.codebrand); VM158:1 Uncaught Referenceerror: place is not defined at <Anonymous>:1:13

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.