0

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? 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 hairdresserId within data.

{
  "ref": Ref(Collection("hairdressers"), "328130955075125442"),
  "ts": 1649343283790000,
 data: {
  "hairdresserId": "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 hairdresserId = 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(
      hairdresserId,
      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 (
  hairdresserId,
  image,
  coverImage,
  bio,
  education,
  phone,
  email,
  status,
  preferences
) => {
  return await faunaClient.query(
    q.Update(q.Ref(q.Collection("hairdressers"), hairdresserId), {
      data: {
        image,
        coverImage,
        bio,
        education,
        phone,
        email,
        status,
        preferences,
      },
    })
  )
}

How can I update a document within my hairdressers collection when I don't have the ref but I know that the document contains hairdresserId ?

Mathias Riis Sorensen
  • 504
  • 3
  • 13
  • 34
  • Which part of the referenced answer does not work for you? Did you create an index on the `roleID` field? Also, why not store the entire ref in your `roles` documents? In your example data, the `roleID` differs from the document's documentID... are you relating roles to each other? I ask because your `Update` query doesn't set `roleID`. – eskwayrd Apr 07 '22 at 18:14
  • Hi, eskwayrd, I've changed my description for a, hopefully, better understanding. I'm in doubt about how to add the index part to the fauna function to look up the hairdresserId and then find the collection that I want to update. The hairdresserId is also a user id, so I want a user containing that user id to be able to edit/update the document from the hairdresser collection belonging to the user. – Mathias Riis Sorensen Apr 07 '22 at 18:27

1 Answers1

0

I was able to solve it by replacing this bit within the fauna function:

q.Update(q.Ref(q.Collection("hairdressers"), hairdresserId),

with this:

q.Update(
      q.Select(
        "ref",
        q.Get(q.Match(q.Index("hairdresser_by_id"), hairdresserId))
      ),

I had to create an index with the following term hairdresserId and I named it hairdresser_by_id.

This is the complete fauna function:

const updateJobProfileInfo = async (
  hairdresserId,
  image,
  coverImage,
  bio,
  education,
  phone,
  email,
  status,
  preferences
) => {
  return await faunaClient.query(
    q.Update(
      q.Select(
        "ref",
        q.Get(q.Match(q.Index("hairdresser_by_id"), hairdresserId))
      ),
      {
        data: {
          image,
          coverImage,
          bio,
          education,
          phone,
          email,
          status,
          preferences,
        },
      }
    )
  )
}
Mathias Riis Sorensen
  • 504
  • 3
  • 13
  • 34