我正在学习编码,现在我正处于一个以 Sanity 作为 CMS 的小型宠物项目的阶段。长话短说,制作一个 API 我正在尝试通过鸡尾酒投票来获取鸡尾酒数据。投票存储在投票的人中:
GROQ 查询
*[
_type == "cocktail" &&
!(_id in path('drafts.**'))
] {
name,
_id,
"votes" : *[_type == "person" && references(^._id)] {
votes[] {
score,
"id": cocktail._ref
}
}
}
返回
[
{
"_id": "pdUGiuRzgLGpnc4cfx76nA",
"name": "Cuba Libre",
"votes": [
{
"votes": {
"id": "pdUGiuRzgLGpnc4cfx76nA",
"score": 2
}
},
{
"votes": {
"id": "pdUGiuRzgLGpnc4cfx76nA",
"score": 2
}
}
]
},
{
"_id": "pdUGiuRzgLGpnc4cfxBOyM",
"name": "The ERSH7",
"votes": []
}
]
如您所见,合并提供了嵌入式投票数组,同时我想要这样的东西:
[{
...cocktail attributes...
"votes" : [
{score: 2, id: pdUGiuRzgLGpnc4cfx76nA},
{score: 2, id: pdUGiuRzgLGpnc4cfx76nA}
]
}
... more cocktails....
]
试图得到这个我修改了查询:
*[
_type == "cocktail" &&
!(_id in path('drafts.**'))
] {
name,
_id,
"votes" : *[_type == "person" && references(^._id)].votes[] {
score,
"id": cocktail._ref
}
}
这应该从投票 arr 的每个元素中进行预测。不幸的是,我得到了空数组:
[
{
"_id": "pdUGiuRzgLGpnc4cfx76nA",
"name": "Cuba Libre",
"votes": [
{},
{}
]
}
...more cocktails
]
我怎样才能达到预期的效果?感谢您的阅读!将不胜感激任何帮助!