Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting a custom document ID in Firebase through Firestore #4

Open
1Marc opened this issue Feb 14, 2019 · 9 comments
Open

Setting a custom document ID in Firebase through Firestore #4

1Marc opened this issue Feb 14, 2019 · 9 comments

Comments

@1Marc
Copy link
Collaborator

1Marc commented Feb 14, 2019

When you recorded the course, this code: firestore.collection('posts').add(post); would use the id property as the document id in firebase. This isn't the case anymore. If you use add(), then Firebase will use a generated document ID by Firebase instead of the id you provided, causing getting the document later with firestore.doc('posts/CUSTOMID') to not retrieve any document.

The way to resolve this and make the course code work as expected, is that you need to create a new document reference "path" with firestore.collection('posts').doc(CUSTOMID) and then you save it to the database with the set method. .. so the full code would be firestore.collection('posts').doc(CUSTOMID).set(post) and post would not contain the id in it's data since it's now the path of the document.

The code we need to update was introduced in the cf06e77ad586367c0cb24ee42a2ed479f2119adc commit and will need to be updated in all subsequent branches.

@1Marc 1Marc changed the title Firebase changed custom ID API? Setting a custom document ID in Firebase through Firestore Feb 14, 2019
@laniehei
Copy link
Contributor

firestore.collection('posts').doc(post.id).set(post) replaced the line referenced above during QA, and works as expected.

@stevekinney
Copy link
Owner

stevekinney commented Feb 28, 2019

Okay @laniehei && @1Marc! I made a new branch called qa-modification:

https://github.com/stevekinney/think-piece/tree/qa-modification

I made the change and then cherry-picked all of the subsequent commits. That should be the safest way to do it.

If it looks good to y'all, I will rename it live-coding or we can even squash that extra commit in.

Sorry about the delay!

@1Marc
Copy link
Collaborator Author

1Marc commented Mar 2, 2019

I think @laniehei links to specific commits in the annotations. So if you rename the branch, all the commit hashes she refers to in the annotations will need to be updated. If squashing the extra commit in isn't too difficult, I think that would be the best approach @stevekinney.

@stevekinney
Copy link
Owner

Sorry for the delay—I totally missed this comment. Thanks for following up on Twitter. I squashed on the qa-modification branch. What I meant to say is that if this works for y'all I will rename it to live-coding and the links should work. I also squashed it in this branch.

But also, I tried to reproduce the issue and I couldn't get it to error out. Everything worked. Am I missing something?

@stevekinney
Copy link
Owner

I pushed up the combined commit and made it the live-coding branch.

@laniehei
Copy link
Contributor

Thanks Steve!

It's interesting that you weren't able to reproduce the issue -- @1Marc cloned the branch at the point were this was written, and was able to recreate the bug on his machine that I was seeing as I was going through the course.

@SwainKomal
Copy link

I'm also working in a project where I've to used a custom I'd but that should be the one inputted by me from a form .
Please someone can guide me how can i do that ?

@FelixEhuan
Copy link

I'm also working in a project where I've to used a custom I'd but that should be the one inputted by me from a form .
Please someone can guide me how can i do that ?

Exactly the same way that were discussed above ^

@lucien-n
Copy link

lucien-n commented Jul 1, 2021

When you recorded the course, this code: firestore.collection('posts').add(post); would use the id property as the document id in firebase. This isn't the case anymore. If you use add(), then Firebase will use a generated document ID by Firebase instead of the id you provided, causing getting the document later with firestore.doc('posts/CUSTOMID') to not retrieve any document.

The way to resolve this and make the course code work as expected, is that you need to create a new document reference "path" with firestore.collection('posts').doc(CUSTOMID) and then you save it to the database with the set method. .. so the full code would be firestore.collection('posts').doc(CUSTOMID).set(post) and post would not contain the id in it's data since it's now the path of the document.

The code we need to update was introduced in the cf06e77ad586367c0cb24ee42a2ed479f2119adc commit and will need to be updated in all subsequent branches.

Kind human, I don't know you but your post saved me from the darkness of going on the page "2" of google, you really are desperate when you try the second page of the google search

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants