What problem is occurring in Axios with this HTTP call?

Asked

Viewed 197 times

0

I’m trying to make a call HTTP, but as a beginner I’m not able to find the error.. I already tested it on the emulator and the error which is displayed as follows:

0, _loginService.autenticacao) is not a function. (In '(0, _loginService.autenticacao)(email,senha)','(0, _loginService.autenticacao)' is undefined)

api.js

import axios from "axios";

export const api = axios.create({
    baseURL: "http://localhost:8080/Proj_MedPag/api"
});

loginService.js

import { api } from "./api";

 export  const loginService = {
   autenticacao
 };

function autenticacao(email, senha) {

  let requestOptions = {
    headers: {
      "Content-Type": "JSON (application/json)"
    }
  };

  let data = { "email": email, "senha": senha };

  api.post("/autenticacao", JSON.stringify(data), requestOptions)
    .then(response => {
      //console.log(response.data);
      //return response.data;
      return response;
    })
    .catch(err => {
      throw err;
    });
}

index js.

import React, { Component } from "react";
import { StatusBar, View, TextInput, Alert } from "react-native";

import {autenticacao} from "../../services/loginService";

//import { Actions } from 'react-native-router-flux';

import login from "./login";
import Header from "../../components/HeaderLogin";
import Button from "../../components/Button";

class App extends Component {
  static navigationOptions = {
    header: null
  };

  constructor(props) {
    super(props);

    this.state = {
      email: "",
      senha: ""
    };
  }

  btnEntrar = () => {
    // Alert.alert('Aviso', this.state.email + " - " + this.state.senha);
    //Actions.home();
    //this.props.navigation.navigate("Home");
    const {email, senha} = this.state;    
   const resultado = autenticacao(email, senha);
    Alert.alert('Aviso', resultado);
  };

  btnRecuperar = () => {
    this.props.navigation.navigate("Recuperar");
  };

  btnRegistro = () => {
    this.props.navigation.navigate("Registro");
  };

  render() {
    return (
      <View style={login.container}>
        <StatusBar
          barStyle="dark-content"
          backgroundColor={login.container.backgroundColor}
        />
        <Header />

        <View style={login.boxcampos}>
          <TextInput
            style={login.txtcampo}
            placeholder={"E-mail"}
            onChangeText={email => this.setState({ email })}
            value={this.state.email}
          />
        </View>

        <View style={login.boxcampos}>
          <TextInput
            style={login.txtcampo}
            placeholder={"Senha"}
            onChangeText={senha => this.setState({ senha })}
            value={this.state.senha}
            secureTextEntry={true}
          />
        </View>

        <Button style={login.btnRecuperar} type="recuperar"
          onPress={this.btnRecuperar}>
          Recuperar senha
        </Button>

        <Button onPress={this.btnEntrar}>Entrar</Button>

        <Button type="outline"
          onPress={this.btnRegistro}>
          Cadastre-se</Button>
      </View>
    );
  }
}

export default App;

1 answer

2


You are not exporting the function autenticacao.

Use ...

export function autenticacao(email, senha) {

  • Hello.. I did in the loginService.js file right after importing the api

  • Following a tutorial... it was like this

  • Really... your way worked... strange that in the tutorial worked the other way.

Browser other questions tagged

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