我有一个可以多次放映的电影列表。我决定为用户提供一个为一部电影选择多个日期的选项(sanity studio界面)。
电影的模式如下:
export default {
name: 'movie',
title: 'Movie',
type: 'document',
fields: [
{
name: 'title',
title: 'Title',
type: 'string'
},
{
name: 'dates',
title: 'Dates',
type: 'array',
of: [
{
type: 'datetime',
options: {
dateFormat: 'YYYY-MM-DD',
timeFormat: 'HH:mm',
timeStep: 15,
calendarTodayLabel: 'Today'
}
}
]
},
{
name: 'poster',
title: 'Poster',
type: 'image',
options: {
hotspot: true
}
},
{
name: 'body',
title: 'Body',
type: 'blockContent'
}
],
preview: {
select: {
title: 'title',
date: 'date',
media: 'poster'
}
}
}当前查询:
const query = groq`*[_type == "movie"]{
title,
dates,
poster,
body
}`我需要用GROQ过滤dates数组中包含今天日期的电影
也许我把事情复杂化了,有人会想出更好的办法。
这个想法是为了避免数据库中的重复(一部电影可以播放3-6次)。这是我使用数组的唯一原因
发布于 2019-09-02 14:31:19
这个问题的解决方案应该是:
const query = '*[_type == "movie" && dates match $today]{title, dates, poster, body}'
const today = new Date().toISOString().split('T')[0]
client.fetch(query, {today}).then(result => {
// movies which are showing today
})但是,目前在字符串标记器中存在一个缺陷,该缺陷会削弱日期字符串匹配。在此期间,恐怕您唯一的选择是获取所有电影并过滤客户端。我们希望尽快解决这个问题。
https://stackoverflow.com/questions/57509974
复制相似问题