.where().findOne() produces "404 Document not found!" while .where().find() finds it
See original GitHub issueEnvironment
Nuxi 3.0.0-rc.9 11:37:29 RootDir: /home/user/workspace/my-app/ 11:37:32 Nuxt project info: 11:37:32
- Operating System:
Linux
- Node Version:
v16.17.0
- Nuxt Version:
3.0.0-rc.10-27711996.0ab29f6
- Nitro Version:
0.5.1
- Package Manager:
yarn@3.2.3
- Builder:
vite
- User Config:
preset
,target
,mode
,nitro
,colorMode
,runtimeConfig
,app
,css
,imports
,components
,build
,modules
,vueuse
,content
,experimental
,vite
,directus
- Runtime Modules:
@nuxtjs/tailwindcss
,nuxt-directus
,@nuxtjs/eslint-module@3.1.0
,unplugin-icons/nuxt
,@nuxtjs/svg@0.4.0
,@vueuse/nuxt@9.2.0
,@nuxt/content
,@pinia/nuxt
- Build Modules:
-
Reproduction
I’m running into this issue out of the sudden, so it could also be something on my side rather than a bug, however I also upgraded to the latest nuxt/content version.
create a parametrized route with a page [slug].vue and this query
const post = await queryContent('blog')
.where({slug: route.params.slug})
.findOne();
console.log(post);
Describe the bug
This fails
const post = await queryContent('blog')
.where({slug: route.params.slug})
.findOne();
console.log(post);
with
500
404 Document not found! (/api/_content/query?_params={%22where%22:[{%22slug%22:%22my-slug3%22},{%22_path%22:%22/blog%22}],%22first%22:true,%22sort%22:[{%22_file%22:1,%22$numeric%22:true}]}&previewToken)
while
const posts = await queryContent('blog')
.where({slug: route.params.slug})
.find();
console.log(posts[0]);
Finds the post by it’s slug and prints it.
I don’t see anything in the logs of the application, but only the error page in the browser
Additional context
No response
Logs
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:5 (3 by maintainers)
Top GitHub Comments
Changing your query to
const post = await queryContent().where({ slug }).findOne();
will resolve your issue.When you pass a path (string) into
queryContent
it two special behavior:findOne
), Module will look for the exact match of that path. So in your case, it always returnsblog/index.md
, and combining it with other conditions will not helpfind
) module will look for contents that starts with that path. In your case, all files in the blog directory.The better solution is to pass slug into
queryContent
and let nuxt/content find the content for you, you don’t need to defineslug
on each file@mohong Have you checked my previous comment? https://github.com/nuxt/content/issues/1513#issuecomment-1249258736 If it is not helpful for you, please consider providing a simple reproduction using https://stackblitz.com/github/nuxt/starter/tree/content