Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 14de679

Browse files
Delete individual field feature #975
1 parent 6d81955 commit 14de679

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

demo-ng/app/tabs/firestore/firestore.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<Button text="Update" (tap)="firestoreUpdate()" class="button"></Button>
1111
<Button text="Get" (tap)="firestoreGet()" class="button"></Button>
1212
<Button text="Get nested" (tap)="firestoreGetNested()" class="button"></Button>
13+
<Button text="deleteFields" (tap)="deleteFields()" class="button"></Button>
1314
<Button text="arrayUnion" (tap)="arrayUnion()" class="button"></Button>
1415
<Button text="arrayRemove" (tap)="arrayRemove()" class="button"></Button>
1516
<Button text="Document Observable (SF)" (tap)="firestoreDocumentObservable()" class="button"></Button>

demo-ng/app/tabs/firestore/firestore.component.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,26 +203,53 @@ export class FirestoreComponent {
203203
});
204204
}
205205

206+
deleteFields(): void {
207+
const faveDog = firebase.firestore().collection("dogs").doc("fave");
208+
209+
// let's first add the fields
210+
faveDog.update(
211+
{
212+
field1ToDelete: "foo",
213+
field2ToDelete: "bar",
214+
updateTs: firebase.firestore().FieldValue().serverTimestamp()
215+
})
216+
.then(() => {
217+
// and now remove them (added a timeout of 2s, so we can check the change in the Firebase console)
218+
setTimeout(() => {
219+
faveDog.update(
220+
{
221+
last: "Updated From 'delete'",
222+
field1ToDelete: firestore.FieldValue.delete(),
223+
field2ToDelete: firestore.FieldValue.delete(),
224+
updateTs: firebase.firestore().FieldValue().serverTimestamp()
225+
})
226+
.then(() => console.log("Woofie updated from 'delete'"))
227+
.catch(err => console.log("Updating Woofie from 'delete' failed, error: " + JSON.stringify(err)));
228+
}, 2000);
229+
});
230+
}
231+
206232
arrayUnion(): void {
207233
firebase.firestore().collection("dogs").doc("fave")
208234
.update({
209-
last: "Updated From arrayUnion",
235+
last: "Updated From 'arrayUnion'",
236+
fieldToDelete: firestore.FieldValue.delete(),
210237
updateTs: firebase.firestore().FieldValue().serverTimestamp(),
211238
colors: firebase.firestore().FieldValue().arrayUnion(["red", "blue"])
212239
})
213-
.then(() => console.log("Woofie updated from arrayUnion"))
214-
.catch(err => console.log("Updating Woofie from arrayUnion failed, error: " + JSON.stringify(err)));
240+
.then(() => console.log("Woofie updated from 'arrayUnion'"))
241+
.catch(err => console.log("Updating Woofie from 'arrayUnion' failed, error: " + JSON.stringify(err)));
215242
}
216243

217244
arrayRemove(): void {
218245
firebase.firestore().collection("dogs").doc("fave")
219246
.update({
220-
last: "Updated From arrayRemove",
247+
last: "Updated From 'arrayRemove'",
221248
updateTs: firebase.firestore().FieldValue().serverTimestamp(),
222249
colors: firebase.firestore().FieldValue().arrayRemove(["red"])
223250
})
224-
.then(() => console.log("Woofie updated from arrayRemove"))
225-
.catch(err => console.log("Updating Woofie from arrayRemove failed, error: " + JSON.stringify(err)));
251+
.then(() => console.log("Woofie updated from 'arrayRemove'"))
252+
.catch(err => console.log("Updating Woofie from 'arrayRemove' failed, error: " + JSON.stringify(err)));
226253
}
227254

228255
firestoreDocumentObservable(): void {

demo-ng/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"nativescript-angular": "^6.1.0",
2626
"nativescript-camera": "^4.0.2",
2727
"nativescript-imagepicker": "~6.0.4",
28-
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-7.1.6.tgz",
2928
"nativescript-theme-core": "~1.0.4",
3029
"reflect-metadata": "~0.1.10",
3130
"rxjs": "~6.0.0 || >=6.1.0",
@@ -43,4 +42,4 @@
4342
"nativescript-dev-webpack": "^0.15.1",
4443
"typescript": "~2.7.2"
4544
}
46-
}
45+
}

docs/FIRESTORE.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,20 @@ query
218218
});
219219
```
220220

221+
### Deleting specific fields in a document
222+
To delete one or more fields in a document, do this (showing two flavors, use whatever you fancy):
223+
224+
```typescript
225+
import { firestore } from "nativescript-plugin-firebase");
226+
const firebase = require("nativescript-plugin-firebase/app");
227+
228+
firebase.firestore().collection("dogs").doc("fave")
229+
.update({
230+
field1ToDelete: firestore.FieldValue.delete(),
231+
field2ToDelete: firebase.firestore().FieldValue().delete(),
232+
});
233+
```
234+
221235
### Adding to (`arrayUnion`) and removing from (`arrayRemove`) Arrays
222236
If your document contains an array field, you can use `arrayUnion()` and `arrayRemove()` to add and remove elements.
223237

0 commit comments

Comments
 (0)