如何在不知道 ref id 的情况下更新 Fauna 中的文档
How to update a document in Fauna without knowing the ref id
我正在尝试更新 Fauna 集合中的文档,但除了在数据库中查找外,我不知道 ref
ID。我一直在看这个 post: 因为我得到了完全相同的错误,但我不知道如何将它实现到我自己的代码中。
我正在尝试更新数据中包含 hairdresserId 的文档。
{
"ref": Ref(Collection("hairdressers"), "328130955075125442"),
"ts": 1649343283790000,
data: {
"hairdresserId": "328027762241568962",
}
}
这是我的 API 文件:
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()
}
这是我的动物群功能:
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,
},
})
)
}
当我没有 ref
但我知道该文档包含 hairdresserId
时,如何更新我的 hairdressers
集合中的文档?
我能够通过替换动物群功能中的这一点来解决它:
q.Update(q.Ref(q.Collection("hairdressers"), hairdresserId),
有了这个:
q.Update(
q.Select(
"ref",
q.Get(q.Match(q.Index("hairdresser_by_id"), hairdresserId))
),
我必须使用以下术语 hairdresserId
创建一个索引,并将其命名为 hairdresser_by_id
。
这是完整的动物群功能:
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,
},
}
)
)
}
我正在尝试更新 Fauna 集合中的文档,但除了在数据库中查找外,我不知道 ref
ID。我一直在看这个 post:
我正在尝试更新数据中包含 hairdresserId 的文档。
{
"ref": Ref(Collection("hairdressers"), "328130955075125442"),
"ts": 1649343283790000,
data: {
"hairdresserId": "328027762241568962",
}
}
这是我的 API 文件:
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()
}
这是我的动物群功能:
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,
},
})
)
}
当我没有 ref
但我知道该文档包含 hairdresserId
时,如何更新我的 hairdressers
集合中的文档?
我能够通过替换动物群功能中的这一点来解决它:
q.Update(q.Ref(q.Collection("hairdressers"), hairdresserId),
有了这个:
q.Update(
q.Select(
"ref",
q.Get(q.Match(q.Index("hairdresser_by_id"), hairdresserId))
),
我必须使用以下术语 hairdresserId
创建一个索引,并将其命名为 hairdresser_by_id
。
这是完整的动物群功能:
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,
},
}
)
)
}