fireship-io / next-firebase-course

Next.js + Firebase - The Full Course
next-firebase-course-git-main.fireship.vercel.app
477 stars 218 forks source link

postRef should fetch "slug" not docId #5

Closed danvoyce closed 3 years ago

danvoyce commented 3 years ago

On this line you're trying to fetch a document by the slug, but this will return nothing unless the slug is the document ID.

This line should simply be replaced with

const postRef = userDoc.ref
      .collection("posts")
      .where("slug", "==", slug)
      .limit(1);

Happy to submit a PR unless I'm missing something?

danvoyce commented 3 years ago

There's actually more to it than this. Just investigating...

danvoyce commented 3 years ago

Ok, I just realised that the posts doc id is the same as the slug.

Mine was a randomly generated Id, hence the issue.

Out of interest, what is the correct query if we did want to find a post by one of the fields, as the one above isn't working.

I believe this works, but requires another index to be created

const postRef = userDoc.ref
      .collection("posts")
      .where("slug", "==", slug)
      .orderBy("slug", "desc")
      .limit(1);