Error using firebase.firestore.Fieldvalue.serverTimestamp() method in an array

Asked

Viewed 118 times

1

I am creating a system of questions and answers, and in this system and this system I need the answers to enter an array with the name of the respondent, the value of their response and the timestamp of the time when the response was sent. Follows the code:

        enviar_resposta: function(id_duvida, index){
            let path = firebase.firestore().collection('database').doc(this.id_cliente).collection('assembleia').doc(this.id_assembleia).collection('duvidas').doc(this.id_duvida)
            let resposta = {                
                respostas : [
                    {   nome: this.nome, 
                        resposta:this.nova_pergunta,
                        data_duvida: firebase.firestore.FieldValue.serverTimestamp()
                        }
                ]           
            }
            path.update({
                resposta : firebase.firestore.FieldValue.arrayUnion(resposta.respostas[0])
            })
        },

My problem is in timestamp, somehow it is not allowing me to insert the timestamp data into the array, it follows the error in question:

Erro Firestore

From what I understand the error is related to some kind of timestamp problem and the fact that it is only allowed to use . set() and . update(). Does anyone know how I can solve this problem?

1 answer

0

I found an answer. Servertimestamp() is a deprecated way to use with arrayunion(), so google is just not using it anymore. To create timestamps through the firestore you only need to use the new Date(). the code will work this way:

        enviar_resposta: function(id_duvida, index){
            let path = firebase.firestore().collection('database').doc(this.id_cliente).collection('assembleia').doc(this.id_assembleia).collection('duvidas').doc(this.id_duvida)
            let resposta = {                
                respostas : [
                    {   nome: this.nome, 
                        resposta:this.nova_pergunta,
                        data_duvida: new Date()
                        }
                ]           
            }
            path.update({
                resposta : firebase.firestore.FieldValue.arrayUnion(resposta.respostas[0])
            })
        },

For more information about the problem access: https://github.com/firebase/firebase-admin-node/issues/449

Browser other questions tagged

You are not signed in. Login or sign up in order to post.