Having trouble getting the API data i care about

I’m trying to get pledge information for my Patreon campaign through the OAuth API system.

I already setup my app to link user accounts to their Patreon ID.
Now i wanna map the rewards from my campaign to patrons who linked their accounts.

So i call this function:

module.exports.getRemoPledgeData = async () => {
  const { creatorAccessToken, campaignId } = require("../config/server");
  const client = patreon.patreon(creatorAccessToken);
  const result = await client(`/campaigns/${campaignId}/pledges`);
  const { store } = result;
  const pledges = store.findAll("pledge").map(item => {
    return item.serialize();
  });

  const rewards = store.findAll("reward").map(item => {
    return item.serialize();
  });

  return { pledges: pledges, rewards: rewards };
};

I’m trying to make a separate call to see who has contributed to which rewards, except there is no clean way i can find to make sure I’m only getting data for my campaign. What’s worse, is that printing the rewards doesn’t seem to have relevant info that allows me to ID them without doing even more sorting and digging. When i say “ID them”, i don’t mean the id number itself, i mean other descriptive information like the title so i can actually make sure I’m getting what i want. Like I had to go into dev tools and search through components to find my campaign ID. I feel like i almost have to do that again for the pledges to make sure i’m mapping the right id’s to the right thing.

On top of that, i can’t seem to properly access much of the nested data.

For example: console.log(reward.data.relationships) works,
But console.log(reward.data.relationships.campaign.data.id) does not.

Is there a straight forward way to just get contributors to my campaign, and print the associated rewards? It’s really hard for me just to find out how to get the information i need.

I’m still pretty new to node, so there is probably other stuff I’m not getting.
Here is my repo for reference: https://github.com/jillytot/remote-control/blob/dev/src/modules/patreon.js
Live project is here: https://remo.tv
Thanks!

1 Like

So long story short, i didn’t understand how JsonAPIDataStore worked exactly.
I still think there is an odd combination of too much data, but also not enough of the right data going on.

My disorganized solution is:

module.exports.getRemoPledgeData = async () => {
  const { creatorAccessToken, campaignId } = require("../config/server");
  const client = patreon.patreon(creatorAccessToken);
  const result = await client(`/campaigns/${campaignId}/pledges`);
  const { store } = result;
  const pledges = store.findAll("pledge");

  return pledges;
};

const test___getRemoPledgeData = async () => {
  const { getRemoPledgeData } = require("../modules/patreon");
  const { campaignId } = require("../config/server");
  const pledges = await getRemoPledgeData();

  try {
    pledges.map(pledge => {
      if (pledge.creator && pledge.patron && pledge.reward) {
        console.log("Patron Id", pledge.patron.id);
        console.log("Reward Title", pledge.reward.title);
        console.log("Reward ID: ", pledge.reward.id);
        console.log("Campaign Id", pledge.reward.campaign.id);
        console.log(pledge.reward);
        if (pledge.reward.campaign.id === campaignId) {
          console.log("Save Data");
        }
      }
    });
  } catch (err) {
    console.log(err);
  }
};

No need to .serialize() anything.

1 Like