0

I have a Firebase database with a list of entries. Now, on click, I want to copy a certain document from this database to a user database (create new document in user DB) while at the same time updating one of the fields. I figured out how to copy the document, but I have problems with updating the field. I think I'm simply using a wrong syntax, but couldn't find this example in Firebase documentation.

This is how the original document looks like: (https://i.stack.imgur.com/ux18x.png)]

And this is how it's copied into the user database: (https://i.stack.imgur.com/QsBVy.png)]

I use this code to make the copy: setDoc(doc(userSentencesRef, newSentenceIDText), ...newSentence)

And I tried many ways to update the "Level" field, but none of them works:

setDoc(doc(userSentencesRef, newSentenceIDText), ...newSentence, {data: {LEVEL: "exclude"}})

setDoc(doc(userSentencesRef, newSentenceIDText), ...newSentence, {LEVEL: "exclude"})

setDoc(doc(userSentencesRef, newSentenceIDText), {...newSentence, LEVEL: "exclude"})

(the first two do nothing, the last one adds a separate level field).

I also wouldn't want to list every single field that needs to be added { EN: data.EN, DE: data.DE }, etc, because I want to make sure it works even if the other fields change in future.

I will really appreciate your help.

  • Can you share the document you followed to copy documents? Meanwhile check this similar [thread](https://stackoverflow.com/q/61960613/18265570) – Roopa M Dec 10 '22 at 10:11
  • Hi, I'm sorry I'm not sure what you are referring to? I included a screenshot of my firebase document in the post (sorry I'm obviously very new here)... – u4ii4y984484y203r Dec 14 '22 at 09:50
  • you mentioned **I figured out how to copy the document** from which reference you found about copying documents? – Roopa M Dec 14 '22 at 10:51
  • https://firebase.google.com/docs/firestore/manage-data/add-data I used the Firebase documentation here, but I can't find an example where they copy existing data plus modify only one field – u4ii4y984484y203r Dec 14 '22 at 11:47
  • To update a specific field in a document, you can try the `update` method on a document reference – Roopa M Dec 14 '22 at 13:31
  • hi there. The problem is I am not trying to update a document, because it does not exist yet in the new database. So I want to create a new entry based on an existing entry in another database and at the same time change one of the value. I cannot update something that was not yet created... or am I understanding it wrong? – u4ii4y984484y203r Dec 14 '22 at 13:36
  • How about using [transactions](https://firebase.google.com/docs/firestore/manage-data/transactions#transactions) to copy document and update the field at the same time? – Roopa M Dec 15 '22 at 09:40

0 Answers0