2

I'm quite new to making my own API's and backend. I've set up an express server that has an API call to Salesforce that returns (in the terminal):

{      
  attributes: {
    type: 'Opportunity',
    url: '/services/data/v42.0/sobjects/Opportunity/xxxxxxxxxxxx'
  },
  Id: 'xxxxxxxxxxxx',
  Name: 'United Oil Standby Generators',
  StageName: 'Closed Won'
}

However, when I try to call the API from inside Nuxt using asyncData, it returns nothing. Here is my asyncData:

asyncData ({ params, error, $http }) {
  return $http
    .$get('/api/salesforce/' + params.id)
    .then((res) => {
        console.log('here we go')
        console.log(res.json)
      return { opp: res }
    })
    .catch((e) => {
      error({ statusCode: 404, message: 'User not found, ' + e })
    })
  
},

When I run the page, my terminal console returns results (since I've asked the express server to do that), but when I use the Vue tools on Chrome or try to access the data, there is nothing in Opp. If I return res.json - it returns as undefined. It should be returning as .json already since I've set it in the backend.

Could anyone see where I'm going wrong here? Like I said - I'm pretty new to working with my own API's.

0

1 Answer 1

1

If you go to let's say, a /subjects/3 path with _id.vue being dynamic, you can use the following to have access to this mocked API.

You can use the following to have it displayed on the Nuxt side

<template>
  <div>
    {{ opp }}
  </div>
</template>

<script>
export default {
  async asyncData({ params, error, $http }) {
    try {
      const res = await $http.$get(
        `https://jsonplaceholder.typicode.com/todos/${params.id}`
      )
      console.log('here we go', res)
      return { opp: res }
    } catch (err) {
      error({ statusCode: 404, message: `User not found, ${err}` })
    }
  },
}
</script>

A working github repo can be found here: https://github.com/kissu/so-nuxt-fetch

I'm not sure if there is something not working with your API, but this is how you handle this on the frontend at least.

Sign up to request clarification or add additional context in comments.

1 Comment

Huh, I got the response from here. Looks like my API isn't working properly. I'm not a backend dev so this is really annoying to fix. I might ask a separate question, thanks for the help kissu!

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.