node.js - 获取满足查询条件的文档 ID

标签 node.js google-cloud-functions google-cloud-firestore

我想获取满足云 Firestore 中给定查询的文档 ID。 我的数据库的架构是这样的 deyaPayusers/AuthId /用户详细信息 电话号码: /钱包 /交易 我知道PhoneNo,那有没有办法得到对应的AuthId。 我按照下面的描述实现了我的想法,但遇到了这个错误

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

最佳答案

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

您没有正确插入此字符串。

你需要

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

此外,如果您是从谷歌访问这里的。 .document 的任何非字符串参数也会导致此异常。

关于node.js - 获取满足查询条件的文档 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48060402/

相关文章:

javascript - 如何知道 Mongoose 的 upsert 是否创建了新文档?

javascript - 消费者和 vendor 的 LTI 合规示例

node.js - 如何跨 Web 和 native 应用程序实现数据结构到多个客户端的单向同步?

angular - 如何在 Angular 组件中正确使用 Cloud Functions?

javascript - 如何删除 Firebase 对象映射内的深度嵌套字段

node.js - 如何在 electron 应用程序 windows/mac 中检查设备连接到哪个网络和 VPN?

javascript - 有什么方法可以从 Angular 组件使用 onCall Firebase 函数吗?

google-cloud-platform - 错误 : Page not found The requested URL was not found on this server. 谷歌云功能

javascript - 如何检查 firestore 文档是否有特定字段

node.js - 无法修改云函数中已提交的WriteBatch