Can't connect to Fauna DB

I’m following the learnwithjason.dev tutorial on Netlify-Stripe-Fauna integration for subscriptions. I clone the repo from here.

As I’m following the tutorial from the beginning, I overwrite functions/identity-signup.js with the following code:

const fetch = require('node-fetch');

exports.handler = async(event) => {
  const { user } = JSON.parse(event.body);
  console.log(JSON.stringify(user, null, 2));
  
  const netlifyID = user.id;
  
  const stripeID = 1;
  
  const response = await fetch('https://graphql.fauna.com/graphql',{
  	method: 'POST',
  	headers: {
  		Authorization: 'Bearer ${process.env.FAUNA_SERVER_KEY}',
  	},
  	body: JSON.stringify({
  		query:  `
  			mutation ($netlifyID: ID! $stripeID: ID!) {
  				createUser(data: {netlifyID: $netlifyID, stripeID: $stripeID}){
  					netlifyID
  					stripeID
  				}
  			}
      `,
  		variables: {
  			netlifyID,
  			stripeID,
  		},
  	}),
  })
  
  	.then((res) => res.json())
  	.catch((err) => console.error(err));
  	
  console.log({ response })

  return {
    statusCode: 200,
    body: JSON.stringify({ app_metadata: {roles: ['sub:free'] } })
  };
};

Upon a test signup on the website, I get entries in Netlify ID but nothing in Fauna. I’ve already pasted the Fauna server key into Netlify as an environment variable. What am I missing? Been tearing my hair out for the past 2 days.

Thanks!

Hey there, @witolot :wave:

Thanks so much for reaching out. Can you elaborate further on what you mean by Netlify ID? Additionally, can you please share your site name with us?

I had this issue too. One thing you’ll want to do is to update the fauna link. Go to the Fauna playground you created. I believe this should be the new URL (for const response = await fetch)
https://graphql.us.fauna.com/graphql

Not sure if that will solve everything but it was one of the issues I encountered with that same tutorial.

1 Like

Thanks @desidem. Brilliant! I wasn’t even looking at that. There indeed was a mismatch in the URLs.

Out of curiosity, did you manage to make the tutorial work in the end? What were the other issues you’ve encountered?

Thanks!

Hi @witolot ! I did get it to work but with some changes. For my needs, I didn’t want to load the subscription content into the page. I used a _redirect file instead. So, I didn’t incorporate the ‘get-protected-content’ function and the related code so don’t know if there’s any hiccups there.

Big thing: You’ll also need to watch out for node-fetch. The current version (v3 I believe) isn’t supported. It was suggested to use the older version or to use isomorphic-fetch.

Also, I have in my notes I had an issue with integrating the Stripe billing portal. I think there were 2 new functions files for that (with the fetch module updated). Stripe needs you to verify some items in the Stripe test portal. You have to select what you want to appear in that test portal and save those changes. (The link is in the error statement in the log for that Function Stripe Login | Sign in to the Stripe Dashboard)

And another issue with role updating: The handle-subsription-change.js function uses the Stripe nickname to create the Identity role, but Stripe doesn’t use ‘nickname’ the same way anymore. I found out about that through an earlier Forum post:

So, I got it all to work, but I decided I wanted to use Stripe Checkout instead (I don’t need a free content option, just gated paid content) and now I’m dying with the functions/webhooks related to updating/syncing the Fauna database.

Feel free to reach out again if you need further clarification about that tutorial. I can’t help with the loading content parts, but I solved most of the issues I had regarding the role updating.

1 Like

Hi @desidem, thanks for the very detailed reply! Oh my, I wasn’t even aware of all the issues you’ve worked through already! Let me play around with it some more and see where I get to. I’ll definitely reach out when I hit a wall! Mighty thanks!

2 Likes

I’m also having some issues with my new attempt (integrating Checkout). I can’t get a simple function to work with the Fauna database. The function itself is being called, but I must have written the Fauna mutation wrong because nothing is appearing in the Fauna documents. Very frustrating.

1 Like