Hi everyone, I'm trying to invite people via email...
# off-topic
c
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
Do you have a trigger setup on your
auth.users
table?
c
yes whenever a new user is added, it adds a profile.
s
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
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
Correct
c
Ok, thanks again, will double check what data I pass
s
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
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
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
ah, missed that, thanks!
They are all nullable
s
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
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
You're welcome