0

i have a function that creates and add new data to the cloud firestore! i want the function to check if the documents already exist, if so update them otherwise create them! i do not know how its done. can someone help me with this please! thank you all in advance. this is the function.

static void createPostMonday(Post post) {
    postsRef.document(post.authorId).collection('Monday').add({

      'alOne':post.alOne,
      'altwo':post.alTwo,
      'althree':post.alThree,
      'alFour':post.alFour,
      'alFive':post.alFive,
      'alSix':post.alSix,
      'beOne':post.beOne,
      'beTwo':post.beTwo,
      'beThree':post.beThree,
      'beFour':post.beFour,
      'beFive':post.beFive,
      'beSix':post.beSix,
      'likes': post.likes,
      'authorId': post.authorId,
      'timestamp': post.timestamp,
    });
  }
Error Place
  • 45
  • 1
  • 1
  • 8
  • What you have now always adds a new document using a random document ID regardless of what already exists. What condition do you intend to use to determine if some document already exists? You will need to be specific about this, otherwise you will have a very hard time making this work. – Doug Stevenson Mar 24 '20 at 14:55
  • hi Doug. the classes are based on activities that my users get to set weekly! in other words i am looking for a solution that updates the same documents set for monday activities after the users set them once instead of creating new document when users wants to upload next weeks monday activities! @DougStevenson thank you for responsing – Error Place Mar 24 '20 at 15:17
  • You can check **[this](https://stackoverflow.com/questions/59905856/i-need-to-update-document-if-exists-otherwise-create)** out. – Alex Mamo Mar 24 '20 at 16:34
  • hi @AlexMamo. thank you for your answer! i checked out your solution and i am having a hard time implementing it in my code! can you help me understand better? thank you in advance – Error Place Mar 24 '20 at 20:01
  • i am getting error saying (the method setData isn't defined for the class CollectionReference! postsRef.document(post.authorId).collection('Monaday').setData. what am i doing wrong? anyone can help? – Error Place Mar 25 '20 at 14:00

0 Answers0