【问题标题】:How do you get the wildcard auto id parent of a document snapshot firestore onCreate function?如何获取文档快照 Firestore onCreate 函数的通配符 auto id parent?
【发布时间】:2020-08-07 12:13:06
【问题描述】:

我查看了类似问题的类似答案,但这些答案并不特别适用。 我在 js 的 firestore onCreate 函数中寻找简单的文档父自动 ID?

firestore 函数日志读取函数返回 undefined for documentiD 你如何引用documentID?

Firestore 日志结果:sendMailtransaction 函数返回未定义、预期的 Promise 或值


//Send the transaction email
exports.sendMailtransaction = functions.firestore
    .document('Companys/{companyid}/Transaction/{transactionid}')
    .onCreate((snap, context) => {

        const transDocument = functions.firestore.document('Companys/{companyid}/Transaction/{transactionid}');
        const documentiD = transDocument.documentID;


        const mailOptions = {
            from: 'L App<report@sample.com>',  // You can write any mail Address you want this doesn't effect anything,
            to: snap.data().companyemailcf, // This mail address should be filled with any mail you want to read it,
            bcc: 'admin@l.com',
            subject: 'L Record, New Tranaction ',
            html: `<h1>Confirmed Transaction</h1>
                                    <p>
                                       <b>Ref: </b>${documentiD}<br>
                                       <b>Datetime: </b>${snap.data().datetime}<br>
                                       <b>User: </b>${snap.data().user}<br>
                                       <b>Vehicle: </b>${snap.data().vehicle}<br>
                                       
                                    </p>`
        };
    }

【问题讨论】:

    标签: google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您无需查询创建的文档即可在函数中接收它,因为您已经在 onCreate 参数中有该文档:snapshot 是第一个参数。

    例子:

    export const eventCreated = functions.firestore
        .document('/events/{eventId}')
        .onCreate(snapshot => {
            console.log("this is the new document id: ", snapshot.id)
            console.log("Document content:", snapshot.data())
        })
    

    在你的情况下,使用snap.id

    Firebase 函数指南是 here
    快照参考解释here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-02
      • 2020-12-16
      • 2020-10-12
      • 2018-03-19
      • 2023-03-19
      • 2019-02-18
      • 1970-01-01
      • 2020-03-09
      相关资源
      最近更新 更多