samuelgozi/firebase-firestore-lite

Delete field inside a document

Kholid060 opened this issue ยท 3 comments

I'm trying to delete a field inside a document, I found this in the official library. And how can I do that using this library?

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

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

@Kholid060 Hi and thanks for opening an issue!
On this library the way you do that is by setting the field you want to remove to the value undefined to the update method:

const ref = db.ref('users/samuel');
ref.update({
  age: undefined
})

For this reason, when you don't want to a field included in the changes to the document, make sure to use the delete operator(as opposed to setting it to undefined):

const changes = {
  name: 'Samuel',
  ocupation: 'Web-Developer'
};

// If I only want to update the "occupation" field I need to delete the "name" field
// by using the "delete" operator, and not by setting it to undefined(as some people do).
delete changes.name;

const ref = db.ref('users/samuel');
ref.update(changes);

Please let me know if something is not clear or if you have anny suggestions.

I will try it soon and thank you for the great library.