1

I have a Rails 5.2.2 API that is serving JSON data at localhost:3000/v1/stories. I am trying to set up a Vuejs app to consume it with Axios. The rails terminal shows the request and throws no errors:

Started GET "/v1/stories" for 127.0.0.1 at 2018-12-19 18:06:44 -0500
Processing by V1::StoriesController#index as HTML
  Story Load (0.8ms)  SELECT "stories".* FROM "stories"
  ↳ app/controllers/v1/stories_controller.rb:6
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::JsonApi (6.25ms)
Completed 200 OK in 10ms (Views: 8.7ms | ActiveRecord: 0.8ms)

MyIn the vue app, I have the following relevant files:

Api.js retrieves the base connection:

import axios from 'axios'

export default() => {
  return axios.create({
    baseURL: `http://localhost:3000/v1`
    headers: {
      'Accept': 'applicaton/json',
      'Content-Type': 'application/json'
    }
  })
}

StoriesService.js:

import Api from '@/services/Api'

export default {
  fetchStories () {
    return Api().get('/stories')
  }
}

and in the view (components/Stories.vue):

<template>
  <div class="stories">
    <h1>Stories</h1>
    <div v-if="stories" class="table-wrap">
      <div v-for="story in stories" :key="story.id">
        <h3>Title: {{story.title}}</h3>
        <p>Summary: {{story.description}}</p>
      </div>
    </div>
    <div v-else>
       There are no stories... Let's add one now <br><br>
       <!-- <router-link v-bind:to="{ name: 'NewStory' }" class="add_story_link">Add Story</router-link> -->
    </div>
  </div>
</template>

<script>
import StoriesService from '@/services/StoriesService'
export default {
  data () {
    return {
      stories: []
    }
  },
  mounted () {
    this.getStories()
  },
  methods: {
    async getStories () {
      const response = await StoriesService.fetchStories()
      this.stories = response.data.stories
    }
  }
}

</script>

In the Rails app, cors.rb is:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*',
      :headers => :any,
      methods: %i(get post put patch delete options head)
  end
end

I made the suggested adjustment. here is the response from the GET request to the back end in the. It shows the data coming through, but for some reason, it does not display from the v-for loop. enter image description here

6
  • Can you please describe the problem? Are there any errors in your browser console? Also check your browser's Network console; what does the response look like there?
    – Phil
    Dec 20, 2018 at 3:06
  • If I guess, I would say your API returns an array and not an object with stories property in which case, you should have this.stories = response.data
    – Phil
    Dec 20, 2018 at 3:11
  • Hey Phil, thanks. I made the change you suggested and the response in the network tab (screenshot above) shows the data, but for some reason, it is not being displayed in the view. Dec 20, 2018 at 13:14
  • Right, looks like you actually need this.stories = response.data.data (if I'm reading your screenshot correctly)
    – Phil
    Dec 20, 2018 at 19:12
  • Wow. I still don't see yet where the extra 'data' comes from, but that works! If you put this in an answer, I will accept. Thanks! Dec 20, 2018 at 19:36

1 Answer 1

1

Your Rails API endpoint is returning the stories array assigned to a data property, eg

{
  "data":[...]
}

The response body object is assigned to response.data via Axios so what you actually want is

this.stories = response.data.data

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.