Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda