1

I'm trying to update the document with the new comments array but I'm getting this error when I submit a new comment. In every document, there is an array(comments array) of object(single comment)

FirebaseError: Function updateDoc() called with invalid data. serverTimestamp() is not currently supported inside arrays (found in document solutions/wLflWVhLueEKjBBknHNi)

comment submit code:

const handleSubmit = async (e) => {
    e.preventDefault()

    try {
      const commentToAdd = {
        id: Math.floor(Math.random() * 10000),
        content: newComment.trim(),
        reactions: [],
        user: {
          userID: user.uid,
          avatarURL: user.photoURL,
          displayName: user.displayName,
          username: user.reloadUserInfo.screenName,
        },
        replies: [],
        createdAt: serverTimestamp(),
      }
      await updateDocument(id, {
        comments: [...solution.comments, commentToAdd],
      })
      setNewComment("")
      if (response) {
        console.log(response.error)
      }
    } catch (error) {
      console.log(error)
    }
  }

updateDocument hook code:

  const updateDocument = async (id, updates) => {
    console.log(JSON.stringify(updates))
    dispatch({ type: "IS_PENDING" })
    try {
      const updatedDocument = await updateDoc(doc(db, c, id), updates)
      dispatchIfNotCancelled({ type: "UPDATED_DOCUMENT", payload: updatedDocument })
      return updatedDocument
    } catch (error) {
      dispatchIfNotCancelled({ type: "ERROR", payload: error })
      return null
    }
  }

Anyone please help me with this.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
r121
  • 2,478
  • 8
  • 25
  • 44
  • As the error says, you cannot use `serverTimestamp()` in side an array. You can store comments in a sub-collection instead of an array that'll also prevent you from reaching 1 MB max size of document if there are many comments. – Dharmaraj May 06 '22 at 15:53
  • So, I need to add comments sub-collection to every document that I have in the solutions collection. – r121 May 06 '22 at 16:03
  • Yes, that's one way you could do. I am not sure about you DB structure but let's say it's a posts collection and you have comments on it, then you could do `posts -> postId -> comments -> commentId` where every comment would be a document on it's own – Dharmaraj May 06 '22 at 16:07
  • Can you please suggest some resources to learn about sub-collections? I have never used sub-collections before. – r121 May 06 '22 at 16:08
  • Yes, checkout Firestore's [documentation](https://firebase.google.com/docs/firestore/data-model#subcollections), [this video by Firebase](https://www.youtube.com/watch?v=o7d5Zeic63s) and [Firestore - proper NoSQL structure for user-specific data](https://stackoverflow.com/a/68827421/13130697) as an example. If you could share your use case then there might be some similar posts. – Dharmaraj May 06 '22 at 16:10
  • This is my website: https://www.codingspace.codes/. I'm trying to add a comments section on the solution details page(https://www.codingspace.codes/solution/wLflWVhLueEKjBBknHNi), so users can comment on each solution. I have a solutions collection and in the solutions collection, I have a document for each solution that contains all the information related to the solution including comments associated with each solution. – r121 May 06 '22 at 16:31

1 Answers1

3

Instead of:

createdAt: serverTimestamp()

Use this:

createdAt: Timestamp.now()

You can find more info here