'FirebaseError: Function updateDoc() called with invalid data. serverTimestamp() is not currently supported inside arrays
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.
Solution 1:[1]
Instead of:
createdAt: serverTimestamp()
Use this:
createdAt: Timestamp.now()
You can find more info here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | salazarkendall |