0

When fetching from an API (3rd party, I authenticate and get the data in my Laravel Controller) I get 'undefined' in the console. I want to store the data in a Vue component.

I tried a bunch of different things, including a get instead of fetch, but this also logged undefined. I did some research and read about arrow functions but I am not using an arrow function.

data() {
    return {
        tickets: [],
    };
},

created() {
    this.fetchTickets();
},

methods: {
    fetchTickets() {
        fetch('/api')
        .then(res => {
            this.tickets = res.json;
        })
        .then(res => {
            console.log(res.json);
        }) 
    }
}

So, what I want is the collection made out of the get request I send to a 3rd party API in PHP which is returned to the route /api, stored in my Vue component. Now it just logs undefined.

EDIT Backend request in PHP

 $response = $client->get('v1/tickets/search.json', [
        'query' => ['statuses[]' => 'active', 'assignedTo[]' => 314955, 
        'sortDir' => 'desc', 'sortBy' => 'updatedAt']
    ]);

    $json = (string)$response->getBody()->getContents();
    $decoded = json_decode($json, true);
    return collect($decoded);

Route: Route::get('/api', 'ApiController@getTickets',);

2

3 Answers 3

2

fetch returns a promise containing the response res. (This is just an HTTP response, not the actual JSON.)

To extract the JSON body content from the response, we use the json() method

You can read more about using fetch.

fetchTickets() {
    fetch('/api')
    .then(res => res.json()) //returning a promise To extract the JSON body content from the response
    .then(resJson => {
        this.tickets = resJson
        console.log(resJson);
    }) 
}
Sign up to request clarification or add additional context in comments.

2 Comments

The information I got from the API now is properly logged in the console! Thank you!
Oh I have one more question, how does resJson get defined? Does .then just pass on the contents of res => res.json()?
2

Return your data before going in second promise.

fetchTickets() {
    fetch('/api')
    .then(res => {
        this.tickets = res.json;
        return res;
    })
    .then(res => {
        console.log(res.json);
    }); 

2 Comments

It still says undefined in console.log for some reason
@ScottvanDuin Explained the logic in answer. In first then you need to return res.json() then you will get json object in next then.
2

add the return statement in the first promise

fetch('/api')
  .then(res => {
      return res.json();
   })
   .then(tickets => {
     // tickets is a local variable scoped only here
     console.log(tickets);
   }) 

6 Comments

Tickets isn't defined anywhere, this just errors: "app.js:1792 Uncaught (in promise) ReferenceError: tickets is not defined at app.js:1792"
ticket is a local variable in the second promise scope, you shoudnt' make it globally available because you're relying on some async code to populate the value
But then how exactly would this be stored in the data() of the component? (I'm a noob to vue and javascript on this level)
what's the purpose of the data() method, can't you just pass tickets when you call data(tickets) ?
This way I can mount a prop that will be passed on to a different component, I will be using the data from the API more than once, so I dont want to be fetching individually in each component
|

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.