https://supabase.com/ logo
#off-topic
Title
# off-topic
c

cbert

04/06/2022, 7:30 PM
Hi everyone, I'm trying to invite people via email with 'inviteUserByEmail'. The function I use goes through an array of email addresses:
Copy code
data.forEach(async (invite) => {
        const { error, status } = await supabaseServer.auth.api.inviteUserByEmail(
            invite.invitee_email,
            {
                data: invite
            }
        )

        if (error) {
            console.log('error :>> ', error)
            return {
                status,
                body: { error }
            }
        }
    })
supabaseServer is created as follows:
Copy code
import { createClient } from '@supabase/supabase-js'

const supabaseUrl = process.env.VITE_PUBLIC_SUPABASE_URL
const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY

console.log('supabaseUrl, supabaseKey :>> ', supabaseUrl, supabaseKey)

export const supabaseServer = createClient(supabaseUrl, supabaseKey)
Env variables are correctly passed, but when I run the invite function I get the following error:
error :>>  { message: 'Database error saving new user', status: 500 }
. Any ideas why this could happen?
s

silentworks

04/06/2022, 7:34 PM
Do you have a trigger setup on your
auth.users
table?
c

cbert

04/06/2022, 7:35 PM
yes whenever a new user is added, it adds a profile.
s

silentworks

04/06/2022, 7:39 PM
The issue is to do with the trigger
You aren't passing some required additional data that is necessary for creating a profile or something.
c

cbert

04/06/2022, 7:42 PM
ah, I see, so it works when I sign up since I pass seemingly all the necessary info but not when I run the invite, correct?
s

silentworks

04/06/2022, 7:42 PM
Correct
c

cbert

04/06/2022, 7:42 PM
Ok, thanks again, will double check what data I pass
s

silentworks

04/06/2022, 7:43 PM
An invite is creating a record inside of the
auth.users
table, this will then start off the trigger you created because it is triggered when a new record is added to the
auth.users
table
c

cbert

04/06/2022, 7:44 PM
ok, but weirdly non of the fields in my profiles are nullable (besided the id, which get generated)
Or do I have to go about it completely differently?
I also had a look at your repo: https://github.com/silentworks/waiting-list
But since this does of course not show the db side, I'm not sure if you also have a trigger.
s

silentworks

04/06/2022, 7:53 PM
The db side can be seen in the migrations directory, the schemas, functions and trigger creation code is there
If none of the fields are nullable then you need to provide value for those fields else the trigger won't be able to create the entry inside the profiles table
c

cbert

04/06/2022, 7:55 PM
ah, missed that, thanks!
They are all nullable
s

silentworks

04/06/2022, 7:56 PM
What does the
invite
object contain in your
data: invite
part of the code?
Can you make sure the values of that object are matching up to whatever your trigger function is looking for
c

cbert

04/06/2022, 7:59 PM
they don't match up, I just saw that you check for in your code if full_name is there: values (new.id, new.raw_user_meta_data->>'full_name', ...
I don't so, I assume it expects that, and it's not there in the invite. So seems to be the culprit.
Once again, your help did help 😄
Thanks again!
s

silentworks

04/06/2022, 8:06 PM
You're welcome