Function inside map returning Undefined

Asked

Viewed 35 times

-1

I have a map function that needs to confirm if certain values exist so that certain gaps can be filled.

The function that tests if the values exist was created but when returning the value inside the map it returns as Undefined, the console.log() inside the function assures me that it is working but in the map it is going as Undefined.

Response.data.items comes from a google database where I am using map to retrieve certain data, as not all data has the item.pagemap.localbusiness, I created an if that checks if it exists before trying to use the map on it. Follow the map code:

placesInfo = [...response.data.items.map((item) => {
        const isExist = (obj, src) => {
            console.log (obj.map(item => item[src])) //Esse console.log me garante que a função está funcionando
            return obj.map(item => item[src])
        }
        if(!item.pagemap.localbusiness) {
            return {
                title: item.title,
                link: item.link,
                snippet: item.snippet,
                name: '',
                email: '',
                telephone: '',
                address: ''
            }
        }
        else return {
            title: item.title,
            link: item.link,
            snippet: item.snippet,
            name: isExist(item.pagemap.localbusiness, 'name') ? item.name: null, //Aqui o valor sempre vem como undefined
            email: isExist(item.pagemap.localbusiness, 'email') ? item.email: null, //Aqui o valor sempre vem como undefined
            telephone: isExist(item.pagemap.localbusiness, 'telephone') ? item.telephone: null, //Aqui o valor sempre vem como undefined
            address: isExist(item.pagemap.localbusiness, 'address') ? item.address: null, //Aqui o valor sempre vem como undefined
        }})]

At the end of the map, I have an array with results like this:

{
    title: 'Cláudio Souza Barbearia - Barbearia em Muriqui',
    link: 'https://claudio-souza-barbearia.negocio.site/',
    snippet: 'Cláudio Souza Barbearia. Barbearia em Muriqui. Abre amanhã às 09:00. \n' +
      'Solicitar cotaçãoLigar agoraVer rotasWhatsAppEnvie-nos uma mensagemEntrar \n' +
    name: undefined,
    email: undefined,
    telephone: undefined,
    address: undefined
  }

However, the.log console upstairs assures me that there are results to be filled in where we have Undefined, the.log console returns things to me like:

    [
  'Wagner Salão de Beleza Masculino',
  'Wagner Salão de Beleza Masculino',
  'DILMA ESTÉTICA.COM',
  'BARBEARIA COPACABANA - COPACABANA',
  'SALAO E BARBEARIA INTERNACIONAL',
  'BARBEARIA NOSSA SENHORA DA PENHA',
  'BARBEARIA ATLÂNTICA LTDA',
  'BARBEARIA BLUE STAR',
  'BARBEARIA TRÊS AMIGOS LTDA',
  'AUGUSTO GUEDES ALVES',
  'VICENTE PINHEIRO DOS PASSOS',
  'BARBEARIA',
  'CABELEIREIRO KAIOS',
  'SALAO ELITE BARBEARIA LTDA',
  'IRMÃOS FERNANDES BARBEARIA LTDA',
  'SALÃO NOSSA SENHORA PAZ BARBEARIA',
  'SALÃO E BARBEARIA PAPISO'
    ]
    [ 'Barbearia Rodrigues' ]

Any possible solution?

  • 1

    Please elaborate a [mcve] to demonstrate the problem cited. In the code snippet you put in the question, many objects have not been defined and the functions do not exist, which makes it impossible to reproduce the cited error.

  • I updated the post, see if it’s a little clearer, please

  • What struck me is that in that expression isExist(item.pagemap.localbusiness, 'name') ? item.name: null will always return item.name, for isExist() the result is always an array, even if empty, and the operator ? will induce the result of isExist() a boolean. Well in javascript an independent array of being or not empty is always converted to boolean true : example console.log([]) . Which implies that item.name, item.email, item.telephone and item.address are not defined.

1 answer

1


if you’re returning undefined is actually pq is not defined. By its ternary operator. isExist is returning true and they are trying to access the content of item. otherwise I wouldn’t have undefined and yes null. If the console.log inside isExists is receiving the object item.pagemap.localbusiness and accessing the Keys inside it. you should be using item.pagemap.localbusiness instead of item in his return or use the return of isExists instead of just using it to check on the ternary operator. Another problem is that you seem to have an array of objects with Keys the same as you want. So isExists is actually returning a simple array with all the key values you searched for.

Assuming you want to save all the values, you should return all the content that isExists returns. If this is your case, replace it with the syntax below:

else return {
  title: item.title,
  link: item.link,
  snippet: item.snippet,
  name: isExist(item.pagemap.localbusiness, 'name') || null,
  email: isExist(item.pagemap.localbusiness, 'email') || null,
  telephone: isExist(item.pagemap.localbusiness, 'telephone') || null,
  address: isExist(item.pagemap.localbusiness, 'address') || null,
}
  • That is important ...isExist is returning true... .

  • It worked perfectly, thank you very much! Would you have some way for me to ensure that in the answer an array does not come and just take the first value returned?

  • 1

    add [0] after the call of isExists, example: name: isExist(item.pagemap.localbusiness, 'name')[0] || null

  • @Arthurlopes Consider accepting the answer as the solution if you have answered your question.

  • Everything worked out here, thank you very much, really!

Browser other questions tagged

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