samuelgozi/firebase-firestore-lite

Creating new documents in a transaction

Closed this issue ยท 10 comments

I want to have a "userspace" when creating a doc in firebase. What i'm doing right now for the collection users is the following:

tx.set(`/users/${auth.user.localId}`, { name: 'foo', email: 'foo@bar.com' });

This works since i only need a document per user.

I need to write on a table called suppliers, but instead of 1 doc i need multiple docs.

tx.set(`/data/suppliers/${auth.user.localId}/1234`, {name: "foo", ref: "bar"});

This works, it creates a doc called 1234 where the parent collection is the uid.
My question is, how do i create a doc with a random id?
This doesn't work:

tx.set(`/data/suppliers/${auth.user.localId}`, {name: "foo", ref: "bar"});

It gives out the following error:
Uncaught (in promise) Error: Expected a Document, Reference or a string path pointing to a document.

Issue-Label Bot is automatically applying the label question to this issue, with a confidence of 0.84. Please mark this comment with ๐Ÿ‘ or ๐Ÿ‘Ž to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

It works as it should(at the moment). /data/suppliers/${auth.user.localId} is a path to a collection, not a document. I assume you want to create a document inside of that collection with a random ID.

If you want to do it outside of a transaction then you can do it with set called on a collection.
However, it doesn't work inside of a transaction because firebase only supports update and delete operations in transactions.

You might be asking "but I saw that on the official SDK", and you would be right. But its because they "fake" it. They generate a random id in the client(which is not a good idea generally).

All that being said. In the beta version, I will push this feature in the next patch and update you with a comment.

If you want to use the beta before the next patch, please note that there are no docs yet(Hopefully I'll update it before the next patch).

Yeah that makes sense, i thought set was the same inside/outside a transaction but ofc it can't be since it can't generate the random id right away (in the transaction).
I'd appreciate if you update me when you push that feature in the beta then!

@arcticlula I will. Im trying to make it work exactly the same inside a transaction.
I wanted to have it ready today, but I've hit a roadblock, so it will take more than I anticipated, but hopefully this week.

I'm using the beta and doing something like this:

import { fid } from 'firebase-firestore-lite/dist/utils.js'; tx.set(`/data/supplies/${auth.user.localId}/${fid()}`, {name: "foo", address:"bar"});

This is working for me now so take you time!
Just one thing, that first issue i opened about the queries throwing an error if there's no results, is present in the latest beta.

Great. I wanted to suggest that but I didnโ€™t want to force you to use the beta.

Only had a problem with the beta and you might want to check that out in your build.
You've used ?? (nullish coalescing operator) in one of the files but i think you forgot to include the babel dependency for it. Check that whenever you can.

@arcticlula RC1 for version 1.0.0 is live.
You can now do ref.add({ ... }) on a collection to add to it with an randomly generated name.
And in transactions, you can do the same: tx.add({ ... }).

They are both the same code now. So less to worry about.
Please let me know if something doesn't work as it should.

Will try it as soon as i can!