Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Document Id's that satisfy the condition in a Query

I want to get document id which satisfies a given query in cloud firestore. The schema for my database is like this deyaPayusers/AuthId /user details PhoneNo: /Wallet /Transaction I know PhoneNo, then is there a way to get the corresponding AuthId. I implemented my idea as described below and I'm encountering this error

Argument "documentPath" is not a valid ResourcePath. Path must be a non-empty string.

const functions = require('firebase-functions');
const Firestore = require('@google-cloud/firestore');
const firestore = new Firestore();
const admin = require('firebase-admin');
    exports.transactionDetails = functions.firestore
        .document('deyaPayusers/{authid}/Transaction/{authid1}')
            .onWrite(event=>{
            const db1 = admin.firestore();
            const MAuth = event.params.authid
            const transid = event.params.authid1
            var payeeDocId
            var newValue = event.data.data();
            var payee = newValue.Payee;//Phone number of money receiver
            var amt = newValue.Amount;//Amount willing to pay to payee(money receiver)
            var usersRef = db1.collection('deyaPayusers').doc(MAuth);//Refers to payer doc
            var payer = usersRef.PhoneNo;//Gets Phonenumber attribute of payer
            const walletRefPayer = db1.collection('deyaPayusers').doc(MAuth).collection('Wallet').doc(MAuth);//Wallet reference of Payer
            var walletAmt = walletRefPayer.Usd//Accessing the total amount of a person(payer) stored in the wallet
            //Getting the payee details assuming, payee has an account in DeyaPay else we need to send the invite to the payee
            var payeeInfo = db1.collection('deyaPayusers');//Query to retrieve the payee details from his phone number
            let payeeQuery = payeeInfo.where('PhoneNo','==',payee)//We are retrieving it here,before checking the condition because we need to update the transaction details with status either it is failure or success
                .get()//To get the doc ID of the Payee based upon  the PhoneNo
                .then(snapshot => {
                    snapshot.forEach(doc=>{
                      payeeDocId = doc.id;


                       }
                        });
                 /*.catch(err =>{
                        console.log('Error getting documents',err);
                        });*/
            //const docID = payeeQuery.getId();//Gets the Document ID of the payee with the above phone number,in our case it is Authenticated ID.
            var payeeWalletRef = db1.collection('deyaPayusers').doc(payeeDocId).collection('Wallet').doc(payeeDocId);
            if(walletAmt>=amt){
                //Write transactions here,becuase both actions(increment and decrement) must complete together
            var payeeTransaction = db1.runTransactions(pT=>{
                    return pT.get(payeeWalletRef)
                        .then(doc =>{
                            var payeeDoc = doc.data(Usd)+amt;//Crediting the amount to be added
                            return pT.update(payeeWalletRef,{
                            Usd:payeeDoc});//end of update
                            })
                })//end of payeeTransaction
            var payerTransaction = db1.runTransactions(ev=>{
                return ev.get(walletRefPayer)
                    .then(doc=>{
                    var payerDoc  = doc.data(Usd)-amt;//Debitting the amount to be transfered
                    return ev.update(walletRefPayer,{
                    Usd:payerDoc});//end of update
                    });//end of then for payerTransaction
                    })//end of payerTransaction
            }
            });//onWrite() end
like image 471
Divya Galla Avatar asked Jan 02 '18 11:01

Divya Galla


People also ask

How do I query Subcollection in firestore?

There is no way to get documents from different collections or sub-collections in a single query. Firestore doesn't support queries across different collections in one go unless we are using a collection group query.

What is cloud collection firestore?

A collection contains documents and nothing else. It can't directly contain raw fields with values, and it can't contain other collections. (See Hierarchical Data for an explanation of how to structure more complex data in Cloud Firestore.) The names of documents within a collection are unique.


1 Answers

.document('deyaPayusers/{authid}/Transaction/{authid1}')

You aren't properly interpolating this string.

You need

.document(`deyaPayusers/{authid}/Transaction/{authid1}`)

Also, if you're coming here from google. Any non-string parameter to .document will result in this exception as well.

like image 126
Brian Rosamilia Avatar answered Nov 10 '22 16:11

Brian Rosamilia