-1

I'm working with Angular Firestore and I've been struggling with this particular task the last two days. Here is what I need: When user taps the save button, I call:

save(group: IGroup){
 if(group.id){
  this.update(group);
 }else{
  this.create(group);
 }
}

So far so good, it works fine when I call update and create methods. My problem is before calling the 'save' function in the first place. I have this schema:

(collection)groups -> (document)group -> (array)group.words

What I need to do is:

if(groups.last.words.length < 9){
  save(groups.last)
}else{
  save(new Group())
}

How can I achieve that by getting groups from Angular Firestore?

Rob
  • 2,243
  • 4
  • 29
  • 40
  • Are you saying you want to fetch a specfic document without knowing the ID of the document? Are you letting Firestore generate the Document IDs? – Nicholas Pesa Apr 23 '18 at 16:27
  • Yes and yes. And I use map and snapshot values to have the document ids. – Rob Apr 23 '18 at 18:21
  • So you do not fetch the documents before making changes to the `(array)group.words`. Is it possible to show any more code of what you are doing before the `save()` method is called? – Nicholas Pesa Apr 24 '18 at 17:49
  • @NicholasPesa thanks for putting some thought on this. I just managed to solve my problem :) – Rob Apr 26 '18 at 14:27

1 Answers1

-1

Well, I solved my problem by declaring:

words: Array<IWord>;

and then populating it within 'subscribe':

this.words$.subscribe( result => {
  this.words = result;
});

Then I got my object from my 'words' array.

Rob
  • 2,243
  • 4
  • 29
  • 40