Angularfire: Update fields in nested objects replaces nested objects

Created on 8 Aug 2018  路  5Comments  路  Source: angular/angularfire

Version info

*Angular:6.1.0*

*Firebase:5.3.1*

*AngularFire:5.0.0-rc.11*

@ionic/angular:4.0.0-beta.1

How to reproduce these conditions

Steps to set up and reproduce

I have FirebaseService which should update appropriate data, in similar way like push works with arrays.
So my constructor:

constructor(public firestore: AngularFirestore) { }

then I have function which make update:
this.firestore.collection(<collectionName>).doc(<docID>).update({'answers.1': <AnswersObject>});

Expected behavior

I should have have answers with ids 0, 1(updated), 2, 3 etc

Actual behavior

I have only 1 object inside answers with id 1

Most helpful comment

Just leaving the "obvious" answer on how to do this using just the native typescript firestore apis for those confused. (I was for like 20 minutes until I stopped being dumb)

import { firestore } from 'firebase';
//...
this.firestore.collection(<collectionName>).doc(<docID>).update({
  answers: firestore.FieldValue.arrayUnion(<AnswersObject>)
});

All 5 comments

For array modifications you should use FieldValue.arrayRemove / arrayUnion; see Todd's blog post and the reference for the JS SDK.

Is there an example of how to do this with angularfire2 or should we use the native SDK?

Just leaving the "obvious" answer on how to do this using just the native typescript firestore apis for those confused. (I was for like 20 minutes until I stopped being dumb)

import { firestore } from 'firebase';
//...
this.firestore.collection(<collectionName>).doc(<docID>).update({
  answers: firestore.FieldValue.arrayUnion(<AnswersObject>)
});

I get a warning when importing all of firestore:

It looks like you're using the development build of the Firebase JS SDK.
When deploying Firebase apps to production, it is advisable to only import
the individual SDK components you intend to use.

For the module builds, these are available in the following manner
(replace <PACKAGE> with the name of a component - i.e. auth, database, etc):

CommonJS Modules:
const firebase = require('firebase/app');
require('firebase/<PACKAGE>');

ES Modules:
import firebase from 'firebase/app';
import 'firebase/<PACKAGE>';

Typescript:
import * as firebase from 'firebase/app';
import 'firebase/<PACKAGE>';

VSCode and all its package management doesn't throw an error when I try:
import { FieldValue } from 'firebase/firestore'; but ng build doesn't like this.

Is there a way to import just FieldValue? Is there a reason why the FieldValue class isn't available as an export?

I get a warning when importing all of firestore:

It looks like you're using the development build of the Firebase JS SDK.
When deploying Firebase apps to production, it is advisable to only import
the individual SDK components you intend to use.

For the module builds, these are available in the following manner
(replace <PACKAGE> with the name of a component - i.e. auth, database, etc):

CommonJS Modules:
const firebase = require('firebase/app');
require('firebase/<PACKAGE>');

ES Modules:
import firebase from 'firebase/app';
import 'firebase/<PACKAGE>';

Typescript:
import * as firebase from 'firebase/app';
import 'firebase/<PACKAGE>';

VSCode and all its package management doesn't throw an error when I try:
import { FieldValue } from 'firebase/firestore'; but ng build doesn't like this.

Is there a way to import just FieldValue? Is there a reason why the FieldValue class isn't available as an export?

Did you find a solution for your question?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DennisSmolek picture DennisSmolek  路  3Comments

itisparas picture itisparas  路  3Comments

StephenFluin picture StephenFluin  路  3Comments

cre8 picture cre8  路  3Comments

Maistho picture Maistho  路  3Comments