0

Can anyone help me what am I doing wrong I can't get objects from this (API key is not in the code). Some Suggestions for how to learn API would be nice. I want to at least console.log() the JSON and go from there.

import React, { Component } from 'react';
import './App.css';


class App extends Component {
  constructor(props){
    super(props);
    this.state ={
      error: null,
      isLoaded: false,
      items: []
    };
  }

  componentDidMount() {
    fetch("https://eun1.api.riotgames.com/lol/summoner/v3/summoners/by-name/EvilDex?api_key=", {mode: "no-cors"})
      .then(res => res.json())
      .then(
        (result) => {
          this.setState({
            isLoaded: true,
            items: result.items
          })
        },
        (error) =>{
          this.setState({
            isLoaded: true,
            error
          })
        }
      )
    
  }

  
Blue
  • 22,608
  • 7
  • 62
  • 92
Pavlin
  • 21
  • 3
  • @Pavlin, if you add `.then(res => { console.log('is ok: ' + res.ok); return res; })` between `fetch()` and `.then(res => res.json())` , what do you see logged in the console? – Dacre Denny Sep 27 '18 at 20:49
  • i see "is ok: false" and an empty array – Pavlin Sep 27 '18 at 21:24

1 Answers1

0

You can't make calls to the API using client-side Javascript. The API doesn't allow it and it also exposes your API key to anyone who inspects your code from the browser.

The process should go like this: React Client requests server for data -> server makes request to Riot API for data -> Riot API responds with data -> Server processes the data (format/sort/filter or whatever you want to do with it) -> Responds to React client with data -> React renders the data in JSX.

mxdi9i7
  • 677
  • 4
  • 13