question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.where().findOne() produces "404 Document not found!" while .where().find() finds it

See original GitHub issue

Environment

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:open
  • Created a year ago
  • Reactions:3
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
farnabazcommented, Sep 16, 2022

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:

  • If you try to find single content (findOne), Module will look for the exact match of that path. So in your case, it always returns blog/index.md, and combining it with other conditions will not help
  • When you try to find multiple contents (find) 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 define slug on each file

const post = await queryContent('blog', ...route.params.slug).findOne();
0reactions
farnabazcommented, Oct 10, 2022

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJS posts.findOne(... Is returning 404 not found
This code here is causing the issue. app. use(function(req, res, next) { var err = new Error("Not Found"); err. status = 404; next(err);...
Read more >
How to Fix Error 404 Not Found on Your WordPress Site - Kinsta
The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is...
Read more >
How to Find and Fix 404 Errors - Neil Patel
A soft 404 happens when a non-existent page on your site displays a “not found” message to users but returns a 200 OK...
Read more >
How to Fix WordPress Error 404 Not Found - DreamHost
A 404 error, also known as a “Page Not Found” error, indicates that your browser can't locate the page you're trying to access....
Read more >
What Does Error 404 Not Found Mean? - Semrush
A 404 error is when a website's server can't find the page you're trying to access via a hyperlink or typing a URL...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found