0

OrderMed.js

export default class OrderMed extends Component {
  constructor(props){
    super(props);
    this.state = {
      Meds: []
    }
  }
  componentDidMount(){
    axios.get("http://localhost:8081/api/v1/ivm/customer/inventory/medicines")
    .then(response => {
      console.log(response)
    })
    .then((data) =>{
      this.setState({Meds: data});
    })

console

{data: {…}, status: 200, statusText: '', headers: {…}, config: {…}, …}
config: {transitional: {…}, transformRequest: Array(1), transformResponse: Array(1), timeout: 0, adapter: ƒ, …}
data:
->medicineList: Array(1)
   ->0: {name: 'Dolo', type: 'fever', price: 50, sellers: {…}}
length: 1
[[Prototype]]: Array(0)
[[Prototype]]: Object
headers: {connection: 'keep-alive', content-type: 'application/json', date: 'Tue, 05 Jul 2022 18:46:55 GMT', keep-alive: 'timeout=60', transfer-encoding: 'chunked'}
request: XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
status: 200
statusText: ""

I need to get that medicineList array in the data, .then((data)={this.setState({Meds: data});}) is not working for some reason

2 Answers 2

1

You are not returning the value from the first then:

componentDidMount(){
    axios.get("http://localhost:8081/api/v1/ivm/customer/inventory/medicines")
    .then(response => {
      console.log(response)
      return response.data
    })
    .then(({medicineList}) =>{
      this.setState({Meds: medicineList});
    })
Sign up to request clarification or add additional context in comments.

3 Comments

response is just to get the actual response from the api server, I need to get that data of medicineList which is a array of objects
See my edit. Hope that helps you.
Meds: data.data.medicineList worked
0
  componentDidMount(){
axios.get("http://localhost:8081/api/v1/ivm/customer/inventory/medicines")
.then(response => {
  console.log(response)
})
.then((data) =>{
  this.setState({Meds: data.data.medicineList});
})

Does this make sense?

1 Comment

I've been doing this at the beginning, but it doesn't until I restart the server it seems, Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.