Skip to content

Instantly share code, notes, and snippets.

@CristalT
Created June 13, 2019 02:49
Show Gist options
  • Save CristalT/a0f47f1005c1a704c655ba6385ca0c4b to your computer and use it in GitHub Desktop.
Save CristalT/a0f47f1005c1a704c655ba6385ca0c4b to your computer and use it in GitHub Desktop.
Ejemplo de cómo buscar la numeración del próximo cliente y crearlo en la Firestore.
const data = this.cliente
const counterRef = this.$firestore.collection('contadores').doc('clientes_motos')
const clientesRef = this.$firestore.collection('clientes/motos/activos')
return this.$firestore.runTransaction(transaction => {
return transaction.get(counterRef).then(counterDoc => {
if (!counterDoc.exists) {
throw new Error('No se encontró el contador de clientes')
}
const next = counterDoc.data().numero + 1
return clientesRef
.doc(next.toString())
.set(data)
.then(() => {
return transaction.update(counterRef, { numero: next })
})
})
}).then(() => {
// hacer algo al finalizar la transaccion
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment