danielwerg / r6api.js

🍫 Node.js wrapper around Rainbow Six Siege APIs
https://npm.im/r6api.js
MIT License
111 stars 19 forks source link

I am stuck pls help (ReactJS) #51

Closed Gipfel closed 3 years ago

Gipfel commented 3 years ago

Hey, I am pretty new on working with APIs and ReactJS and I tried to fetch this API by this React-App and I always get the Error "Unhandled Rejection (TypeError): Failed to fetch", in detail: const id = await r6api.getId(platform, username).then(el => el[0].id); I am thankful for every respond.

import React, { useState, useEffect } from 'react';
import logo from "./logo.svg";
import "./App.css";

function App() {
  const [data, setData] = useState([]);

  useEffect(async () => {
    const R6API = require('r6api.js');
    const r6api = new R6API('********@gmail.com', '********');

    const username = 'GIIIPFEL',
      platform = 'uplay';

    const id = await r6api.getId(platform, username).then(el => el[0].id);
    const stats = await r6api.getStats(platform, id).then(el => el[0]);

    setData(stats);
  }, []);

  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          {data}
        </p>
      </header>
    </div>
  );
}

export default App;
danielwerg commented 3 years ago

There is at least two issue with your code I can spot, first of all, you are suppose to use this package in server side. Second, you can't use async like that in useEffect you need to wrap it inside function

  useEffect(() => {
    const fetchData = async () => {...};
    fetchData();
  }, []);

And error you ran into (Unhandled Rejection (TypeError): Failed to fetch) is sounds like cors issue.

Closing due to not being package issue.