1

So I am building a react on rails project to learn some new things. I am trying to use the fetch API to get data from the db.

I have tried this method and this one. But I might be implementing them wrong.

This is my fetch route:

fetch('/get_data')
      .then(response => {
        response.json();
      })
      .then(data => console.log(data));

I have my route set up in Ruby on Rails:

match '/get_data' => 'get_data#pull', via: :get

I have my controller just doing something simple at the moment to see if I can get any data.

class GetDataController < ApplicationController
  def pull
    @allproduct = Product.all
    render json: @allproduct
  end
end

Thanks for any help in pointing me in the right direction!

Mike Tallerico
  • 125
  • 4
  • 15

1 Answers1

4

The issue was that I needed a return statement in my fetch call. :)

fetch('/get_data')
      .then(response => {
        return response.json();
      })
      .then(data => console.log(data));
Mike Tallerico
  • 125
  • 4
  • 15