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?
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;
}
What would that be
this.value
? I don’t think it refers to anything.– Sam
return place.codebrand.match
-codebrand
will not have the propertymatch
defined. This is easy to confirm by doing before this lineconsole.log(place.codebrand);
– Isac
@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 :(
– Thiago Maia
@Isac I didn’t quite understand your answer, could you tell me which way to solve this? Thanks
– Thiago Maia
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.– Isac
It comes from a json, example: { "codebrand": "6927", "labelbrand": """marca2"" }, { "codebrand": "6921", "labelbrand": "marca2" },
– Thiago Maia
Put a
console.log(place.codebrand);
at the beginning of the functionfunction findMatches(wordToMatch, brands) {
and see what it shows.– Sam
In this example of json you gave,
codebrand
is a number in the form ofstring
and has no propertymatch
.– Isac
It displays the following error > console.log(place.codebrand); VM158:1 Uncaught Referenceerror: place is not defined at <Anonymous>:1:13
– Thiago Maia