1

I'm going to be able to retrieve the data by calling getTicket via listTicket

listTicket :

listTicket()
   {
     objectTicket = this.getTicket();
     objectTicket.map((userData)=>{
      alert(userData.id)
    })
}

getTicket Code:

async getTicket () {
      return await fetch('url', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            uid: '1',
            type: '0'
          })
      })
        .then((response) => {return response.json()})
        .then((responseJson) => {
          return responseJson;
        })
        .catch((error) => {
          alert(error);
        });
    }

I get this error:

typeerror objectTicket.map is not a function

1
  • In order to use map, objectTicket should be an array. But unfortunately you have object instead of array. Commented Aug 27, 2018 at 13:22

4 Answers 4

2

From getTicket method you're getting Promise, so you should to subscribe to it:

listTicket() {
     objectTicket = this.getTicket();
     objectTicket.then((res) => { 
       res.map((userData)=> {
          alert(userData.id)
       })
     })
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, i replaced alert(userData.id) with view but i get error invariant violation, object are not valid, please help
1

The issue here is that getTicket() is a function that returns a Promise whereas map is defined on arrays.

Comments

1

As getTicket is returning a Promise listTicket method should be also async.

async listTicket() {
   objectTicket = await this.getTicket();
   objectTicket.map((userData)=> {
      alert(userData.id)
   });
}

3 Comments

thanks, i replaced alert(userData.id) with view but i get error invariant violation, object are not valid, please help
I don't get your question. Could you paste your code here or deploy your code somewhere, let say on jsfiddle or some similar site. What is it 'view' ?
I added a new post. please guide me
0

An async function returns a promise and resolves with the return value of the function.

So, when you are calling the listTicket function. the listTicket function is returning the promise Object along with the return value{VIEW} and that is creating the problem.

To solve the problem you should set the objectTicket value to a state variable and loop over state variable.

Comments

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.