0

The API I'm using is owapi.net/api/v3/u/Calvin-1337/stats (the name will change). Let's say I wanted the tier, that'd be JSON.us.stats.competitive.overall_stats.tier and I can parse that and get it okay. But now I want to create a promise. Let's make it for the overall_stats so... us.stats.competitive.overall_stats and I only want values from there for the moment. I wan't to be able to do something like:

const core = require("myNodePackage");

core.getCompOverallStats("Calvin-1337").then(data > {
    console.log(data.tier) // grandmaster
    // etc through to
    console.log(data.prestige) // 5
});

This is totally wrong but what I had thought about:

const fetch = require("node-fetch"); // used to get json data

getCompOverallStats = (playerName) => {
    return new Promise((resolve, reject) => {

        // only want this for us.stats.competitive.overall_stats

        fetch("https://owapi.net/api/v3/u/Calvin-1337/stats")
            .then(function(res) => {
                return res.json();
            }).then(function(json) {
                //console.log(json.us.stats.competitive.overall_stats.tier) => grandmaster
            });

1 Answers1

1
getCompOverallStats = (playerName) =>
  // grab the player stats
  fetch(`https://owapi.net/api/v3/u/${playerName}/stats`)
    // parse json
    .then(res => res.json())
    // pull out the one object you want
    .then(data => data.us.stats.competitive.overall_stats);

That should be enough.

You should now be able to call

getCompOverallStats('some-pl4y3r').then(overall => console.log(overall.tier));

Norguard
  • 26,167
  • 5
  • 41
  • 49
  • With this I'm getting an error. `core.getCompOverallStats("Calvin-1337").then(data => { ^ TypeError: Cannot read property 'then' of undefined –  Jul 08 '17 at 13:04
  • Ignore this, I forgot to return it. Thank you. –  Jul 08 '17 at 13:11