Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
prevent firestore number from going nevgative?

I'm New to firestore. I need a little help.
I got a function, that updates the amount of "stock shares". its a "sell" function, so the amount can only go down.
The problem is. I don't wanna go below 0.
So I wanna get the PREVIOUS amount of shares. before I update to the new amount. So I can make sure I don't go below 0.
There are 2 ways to do this. 1 is by using firestore rules, 2 is by getting the Prev amount like i said.
Can you guys help me get the Prev amount before the UPDATE stage? code:
function sellStock(){
    db.collection("Stocks")
    .where("ticker", "==", props.name)
    .get()
    .then((querySnapshot) => {
        if(!querySnapshot.empty){
        querySnapshot.forEach(function(doc){
            db.collection("myStocks")
            .doc(doc.id)
            .update({
                shares: doc.data().shares - amount
            })

"shares" will be the prev amount. "amount" will be the amount of shares we wanna sell.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Try this way:

.update({
            shares: (doc.data().shares - amount) >= 0 ? (doc.data().shares - amount) : 0
       })
about 4 years ago · Juan Pablo Isaza Relatório

0

Updated after discussion in the comments.

There is an important aspect to consider with your business logic: Do you need to execute an atomic operation on several documents? Example: You are subtracting the value of amount to the value of shares, since it is a selling operation but I guess that somewhere else (in another document) you are also adding some value, for example in the bank account of the seller.

In such a case you should use a Transaction: "if a transaction reads documents and another client modifies any of those documents, Cloud Firestore retries the transaction". You need to include in the Transaction all the documents that need to be locked while the operation is ongoing (i.e. all the docs that are involved in the operation, the ones on which you subtract and the ones on which you add).

However, since you want to update several documents, returned by a query, you cannot use a Transaction of one of the mobile/web SDKs (e.g. iOS, Android, Web/JavaScript), because the mobile/web SDKs use optimistic concurrency controls to resolve data contention.

What you can do is to use one of the Admin SDKs, like the Node.js one, since it uses pessimistic concurrency controls and therefore offers the possibility to run a Transaction on a query (see that you can pass a query to the get() method). So you could do that in a Callable Cloud Function.

Here is an example of a Transaction that will atomically update all the docs on which you substract. Since you didn't share the entire business logic (we don't know which are the docs that you need to update by adding a value) it's a bit difficult to go deeper in the example.

exports.updateTickers = functions.https.onCall((data, context) => {

    // get the value of the filter (i.e. props.name) via the data Object
    const filter = data.filter;
    const amount = data.amount;

    const db = admin.firestore();
    return db.runTransaction(transaction => {
        let queryRef = db.collection("Stocks").where("ticker", "==", filter);
        return transaction.get(queryRef)
            .then((querySnapshot) => {
                querySnapshot.forEach((doc) => {
                    const currentValue = doc.get('amount');
                    if (currentValue - amount > 0) (
                        transaction.update(doc.ref, { likes: currentValue - amount })
                    )
                });
            });
    })
        .then(() => {
            return { result: "Amounts update successful" }
        })

});
about 4 years ago · Juan Pablo Isaza Relatório

0

The only sure-fire way to prevent the shares count from going below 0 is to enforce that in security rules:

allow write: if request.resource.data.shares >= 0;

Any other method can be bypassed by a malicious user who uses your project configuration data with their code.


With this in place, the simplest (and fastest) way to subtract the amount from the shares count is with an atomic increment operation:

db.collection("myStocks")
  .doc(doc.id)
  .update({
      shares: firebase.firestore.FieldValue.increment(-1 * amount)
  })
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda