How to update a document in Fauna without knowing the ref id

I’m trying to update a document within a collection in Fauna, but I don’t know the ref id besides looking in the database. I’ve been looking at this post: Can I update a FaunaDB document without knowing its ID? - Stack Overflow as I get the exact same error but I don’t know exactly how to implement it into my own code.

I’m trying to update a document containing a roleId within data.

{
  "ref": Ref(Collection("roles"), "328130955075125442"),
  "ts": 1649343283790000,
 data: {
  "roleId": "328027762241568962",
  }
}

This is my API file:

import { updateJobProfileInfo } from "@/utils/Fauna"
import { getSession } from "next-auth/react"

export default async (req, res) => {
  const session = await getSession({ req })
  if (!session) return res.status(401)

  const roleId = session.user.id
  if (req.method !== "PUT") {
    return res.status(405).json({ msg: "Method not allowed" })
  }

  const {
    image,
    coverImage,
    bio,
    education,
    phone,
    email,
    status,
    preferences,
  } = req.body

  try {
    const updated = await updateJobProfileInfo(
      roleId,
      image,
      coverImage,
      bio,
      education,
      phone,
      email,
      status,
      preferences
    )
    return res.status(200).json(updated)
  } catch (err) {
    console.error(err)
    res.status(500).json({ msg: "Something went wrong." })
  }
  res.end()
}

This is my fauna function:

const updateJobProfileInfo = async (
  roleId,
  image,
  coverImage,
  bio,
  education,
  phone,
  email,
  status,
  preferences
) => {
  return await faunaClient.query(
    q.Update(q.Ref(q.Collection("roles"), roleId), {
      data: {
        image,
        coverImage,
        bio,
        education,
        phone,
        email,
        status,
        preferences,
      },
    })
  )
}

How can I update a document within my roles collection when I don’t have the ref but I know that the document contains roleId ?

Please don’t post the same question here and on StackOverflow: database - How to update a document in Fauna without knowing the ref id - Stack Overflow

Doing so splits the conversation so that anyone providing assistance has to answer in both locations.

The Forums is a better venue for debugging conversations, since SO is more about questions that have definitive answers. I asked several questions on your SO post that might be better answered here.

Thanks for the feedback I’ll keep that in mind. :slight_smile:

I did solve it in the end. It was good learning, but perhaps adding more information on the topic in a tutorial could be great.

As for the solution, I changed the Fauna function. I swapped out

q.Update(q.Ref(q.Collection("roles"), roleId),

with this:

q.Update(
      q.Select(
        "ref",
        q.Get(q.Match(q.Index("role_by_id"), roleId))
      ),

It required me to create an index pointing at the roles collection with the term roleId, which is the one I named role_by_id.

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