Axios: stop in GET does not work - React Native

Asked

Viewed 155 times

2

I have a database in Mongo with customers and establishments.

I want to recover the establishments filtered by category. My code is like this:

const [datas, setDatas] = useState([]) //recupera os dados
const [category, setCategory] = useState('tecnologias') //recupera a categoria selecionada

async function loadDatas(){
   await api.get('/user/read/establishment', {params:{category: category}})
}
.
.
.
{datas.map(d =>(
    <View style={styles.view}>
        <TouchableOpacity onPress={()=>navigation.navigate('Establishment')}>
            < Card title={d.nameEstablishment} time={d.sunday} chave={d._id}/>
        </TouchableOpacity>
    </View>
    ))
}

in params:{category:category} the first Category is metadata from the database establishment table and the second, is what I get from React-Native with a tap on the screen.

It doesn’t work in the app, but in India it does, as much as I’ve seen this example on the official website. I am creating a gambiarra and doing several routes, but it is impractical, because it will have a time when the products will be shown by category and this will not be static, but dynamic (each establishment with its own).

1 answer

1


 async function loadDatas(){
    const res = await api.get('/user/read/establishment',{ <-- add 
        params:{
            category: "tecnologias" <-- coloca aspas e muda o nome
        }
    })
   setCategory(res.data) <-- add, "res.data" vai depender de como que você está retornando
}

following an example like mine --->

async function handleTipo(){
    const res = await api.get('/S881TT75a8s5AaAo2iYtR',{
        params:{
            tipo:'Pasta'
        }
    });
    // console.log(res)
    setDocs(res.data.sens)
}
  • 1

    Thank you for your answer, your way is correct, it works for me. The problem is that my mistake was not the time to read the data in the frontend, but in my database in the backend. Instead of ordering for 'params', I was ordering for 'body'.

  • kkkk thanks and good, managed to solve alone.

Browser other questions tagged

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