Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
API call with React Native and axios

I'm new to React, what I'm trying to do is call my API with a GET request to change the status of an elevator to "Operational" and it's color status from red to green. The problem is that it doesnt seem to call the API and brings me back to my previous screen when I click CONFIRM.

the function for my call to the API looks like this

function ElevatorStatusScreen({navigation, route}) {
const { Elevator } = route.params;
const [elevatorStatus, setEleStatus] = React.useState(Elevator.status);
const [statusColor, setColorStatus] = React.useState('red');


// Call to the api to update the status of an elevator
const endtask = () => {
    axios.get(`https://apilink/api/Elevators/update/${Elevator.id}/Operational`)
    .then(response => {
        if(response.status == 200){
            Alert.alert('Status has been changed to Operational!')
            setColorStatus('green')
            setEleStatus('Operational');
        }
    })
}

return (
    <ImageBackground 
    style={styles.background}
    source={require('../assets/white.jpg')}
    >
        <Text style={styles.title}>Elevator: {Elevator.id}</Text>
        <Text style={{ color: statusColor }}>{elevatorStatus}</Text>          
        {elevatorStatus === 'Stopped' ? (
            <TouchableOpacity style={styles.endTask} onPress={() => endtask()}>
                <Text style={styles.endText}>END TASK</Text>
            </TouchableOpacity>
        )
        :
            <TouchableOpacity style={styles.confirm} onPress={() => {
                navigation.goBack()
            }}>
                <Text style={styles.endText}>CONFIRM</Text>
            </TouchableOpacity>
        }
    </ImageBackground>
);
}

Not sure what I'm doing wrong. This is my endpoint in a C# project coming from my elevators controller. (I tested it and it works)

[HttpGet("update/{id}/{status}")]
public async Task<dynamic> test(string status, long id)
{
    var elevator = await _context.elevators.FindAsync(id);
    
    elevator.Status = status;
    await _context.SaveChangesAsync();         

    return elevator;
}

the endpoint: //apilink/api/Elevators/update/1/Operational returns a JSON like this in my browser

{ "id":1, "status":"Operational", "columnId":1, "serialNumber":"666999990867" }

let me know if you need more code and thanks for your help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem was that my elevatorStatus was 'offline' and NOT 'Stopped' in my database...sorry about that

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda