0

I'm new to Firebase but I understand how it might works but I'm stuck with this for too long and I can't find any doc about it.

I want to update a user from "wallet" field (I don't know his id) and add token with the new amount, so I get the user, the I get again the user to update the data

    const userCollec = await admin.firestore().collection('users').where('wallet', '==', req.data.sender).get();

    const UserToken = userCollec.get('token')
    const UserUsd = userCollec.get('usd')

    admin.firestore().collection('users').where('wallet', '==', req.data.sender).update({
      "token" : UserToken + Math.round(amount / 0.07),
      "usd" :  UserUsd + amount
    })

for me this should work but I don't why it doesn't and I have absolutly no idea.

    const userCollec = await admin.firestore().collection('users').where('wallet', '==', req.data.sender).get();

Those value should return something but they don't even tho the doc with the wallet and the value req.data.sender exist 100%.

    const UserToken = userCollec.get('token')
    const UserUsd = userCollec.get('usd')
nerap
  • 226
  • 1
  • 2
  • 15
  • Firestore doesn't support update queries, where you send conditions and write instructions to the database. You'll have to execute the query, loop over the results and update each individually (or in batched writes/transactions). – Frank van Puffelen Jan 13 '22 at 21:01
  • 1
    So I need to loop over this "await admin.firestore().collection('users').where('wallet', '==', req.data.sender)" then I update the doc – nerap Jan 13 '22 at 21:05
  • That's correct indeed. See the code in my answer here and the ones I linked for example: https://stackoverflow.com/questions/54286111/firestore-function-where-is-not-a-function/54293873#54293873 – Frank van Puffelen Jan 13 '22 at 23:10

0 Answers0