NetAuth - Updating new users

Hey there, I’m using fauna for our NextAuth Provider and I’ve run into some trouble. I try to update a user ref, but it says it’s invalid?

if(message.isNewUser) {
                // Query and add the discord id to the user object
                client.query(
                    q.Update(
                        q.Select(message.user.id,
                            q.Get(
                                q.Match(q.Index("users_by_id"), message.user.id)
                            )
                    ),
                    {
                        data: {
                            discordId: message.account.providerAccountId,
                        }
                    }
                ))
                .then(res => console.log(`[NextAuth]: Updated new user ${message.user.id} (${message.account.providerAccountId}) res: ${res}`))
                .catch(err => console.log(`[NextAuth]: Error updating new user ${message.user.id} err: ${err}`))
            }

[NextAuth]: Error updating new user 330472730502627396 err: BadRequest: invalid ref , but in dash that is a vaild ref. What am I doing wrong?

The Select Function

At this Select function call, you only want to provide the string "ref":

                    q.Update(
                        q.Select("ref",
                            q.Get(
                                q.Match(q.Index("users_by_id"), message.user.id)
                            )
                    )

Get(Match(...)) will return a Document, which has the shape

{
  ref: Ref(Collection("users")),
  ts: 1648149221410000,
  data: { /* ... */ }
}

So when you call Select("ref", SOME_USER_DOCUMENT) it will return the Ref.

Using the Ref directly instead?

If you already have the user’s id, then you can Update the Document directly.

                client.query(
                    q.Update(
                        q.Ref(q.Collection("users"), message.user.id),
                        {
                            data: {
                                discordId: message.account.providerAccountId,
                            }
                        }
                    )
                )

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.