0

Have this code (ApiClient):

import axios from 'axios';

const BASE_URI = 'http://localhost:4433';

const client = axios.create({
 baseURL: BASE_URI,
 json: true
});

class APIClient {
  constructor(accessToken) {
    this.accessToken = accessToken;
  }
  removeUser(id){
    return this.perform('delete', `/removeUser/${id}`);
  }
  checkStatus(uuid){
    return this.perform('get', `/checkStatus?uuid=${uuid}`);
  }




  async perform (method, resource, data) {
    return client({
      method,
      url: resource,
      data,
      headers: {
        Authorization: `Bearer ${this.accessToken}`
      }
    }).then(resp => {
      return resp.data ? resp.data : [];
    })
  }
}

export default APIClient;

In my component

 removeUser = (id)  => {

    const uuid= this.apiClient.removeUser(id);
    console.log(uuid);

    this.setState({ uuid: uuid});
  }

But somehow get a Promise Object with Pending

Prmomise { "pending" }

etc

New to ReactJS and JS world.

What is wrong here? Shouldn't uuid contain response from the DELETE endpoint request?

Patrick Roberts
  • 40,065
  • 5
  • 74
  • 116
dev
  • 915
  • 7
  • 25
  • 3
    `async removeUser = (id) => { const uuid = await this.apiClient.removeUser(id); console.log(uuid); this.setState({ uuid: uuid }); }` Note the added `async` and `await` here. – Patrick Roberts Dec 31 '20 at 20:53
  • index.js: Unexpected token, expected "(" (43:19) – dev Dec 31 '20 at 21:11
  • async removeUser = (id) => { const uuid = await this.apiClient.removeUser(id); console.log(uuid); this.setState({ uuid: uuid }); } – dev Dec 31 '20 at 21:12
  • @PatrickRoberts Thanks for your help. I am a newbie, have an error above now. – dev Dec 31 '20 at 21:12
  • 1
    Apologies, the correct syntax is `removeUser = async (id) => ...`, not `async removeUser = (id) => ...` – Patrick Roberts Dec 31 '20 at 21:13
  • Thanks now it compiles, but still no uuid in the response. It does not even reach the console.log statement (nothing in console). Weird ... – dev Dec 31 '20 at 21:19
  • 1
    That problem sounds unrelated, check your Network tab for HTTP errors and start debugging the HTTP request/response. Are you sending the appropriate headers to the path you intended? Does the response contain what you expect? etc. – Patrick Roberts Dec 31 '20 at 21:22

0 Answers0