【问题标题】:Assigning Field Value of Firestore Document to Const on Cloud Functions将 Firestore 文档的字段值分配给 Const on Cloud 函数
【发布时间】:2017-10-25 15:10:54
【问题描述】:

我想将文档中某个字段的值分配给一个常量,以便在多个函数中使用它。

const stripeAccountId = firestore.doc('orgs/' + subscription.orgId).get()
.then( org => {
    return org.data().stripeAccountId
})

【问题讨论】:

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


    【解决方案1】:

    firestore.doc('orgs/' + subscription.orgId).get().then(...) 方法返回一个promise。更多信息:https://scotch.io/tutorials/javascript-promises-for-dummies

    Promise 是异步的,您需要在 then 内指定的箭头函数内分配 stripeAccountId

    我不知道你会在哪里使用它,但是 stripeAccountId 只有在 promise 解决后才会被填充。

    const stripeAccountId = null; 
    
    firestore.doc('orgs/' + subscription.orgId).get().then(org => {
        stripeAccountId = org.data().stripeAccountId;
    })
    
    console.log(stripeAccountId); // null
    
    const sufficientTimeInMillisToResolveThePromise = 10000;
    
    setTimeout(() => {
      console.log(stripeAccountId); // some-id
    }, sufficientTimeInMillisToResolveThePromise);
    

    【讨论】:

      猜你喜欢
      • 2020-04-25
      • 2019-01-16
      • 2019-10-11
      • 2019-01-04
      • 2021-05-11
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多