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
?