Created
June 13, 2019 02:49
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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