Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查询firestore以检查集合和文档_Javascript_Node.js_Reactjs_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript 查询firestore以检查集合和文档

Javascript 查询firestore以检查集合和文档,javascript,node.js,reactjs,firebase,google-cloud-firestore,Javascript,Node.js,Reactjs,Firebase,Google Cloud Firestore,我对firebase/firestore有点陌生。我正在使用StripeAPI 一旦用户点击预构建条带签出页面上的start-trial,则应转到firestore并创建一个名为subscriptions的新集合,其中包含所有用户信息。它似乎正在这样做,但是,我创建了一个名为successPage的页面,它基本上检查以确保它创建了它 请查找以下代码: const successPage = props => { firebase.auth().onAuthStateChanged((

我对firebase/firestore有点陌生。我正在使用StripeAPI

一旦用户点击预构建条带签出页面上的
start-trial
,则应转到firestore并创建一个名为
subscriptions
的新集合,其中包含所有用户信息。它似乎正在这样做,但是,我创建了一个名为
successPage
的页面,它基本上检查以确保它创建了它

请查找以下代码:

 const successPage = props => { 

firebase.auth().onAuthStateChanged((user) => {
    if(user) {
      console.log("calling success page : " + user.uid)


//checking if user is paying for subscription
firestore.collection('customers').doc(user.uid).collection('subscriptions')
.where('status', 'in', ['trialing', 'active']).get()
.then(activeSubscriptions => {
  // if this is true, the user has no active subscription.
  if (activeSubscriptions.empty === true) {
    console.log(user.uid)
    firestore.collection('customers').doc(user.uid)
    .get().then(
    doc => {
      if (doc.exists) {
        firestore.collection('customers').doc(user.uid).collection('subscriptions').get().
          then(sub => {
            if (sub.docs.length > 0) {
              var activityStatus = "canceled"
              createCheckoutSession(activityStatus)
              console.log('subcollection exists');
            } else {
              alert("Your account has been created, but your payment details we're not successfully created. You will now be redirected to the checkout page")
                createCheckoutSession()
                console.log(user.uid)
                console.log("does not exist!")
            }
          });
      }
    });
  } else if (activeSubscriptions.size > 1){
    alert("you have more then one active subscription. please manage your subscriptions and cancel one of your subscriptions to access the application")
  } else {
    firestore.collection("profiledata").doc(user.uid).update({
      accountStatus: "active"
    }).then (() => {
      
      
      firestore
      .collection("roger@x.ca")
      .add({
        to: user.email,
        message: {
          
        },
      })
      .then(() => console.log("email out for delivery!"));

    props.history.push('/clients')
    
    })
    
  }
});
}

  })

  return (

     <input type="hidden"></input>
  )
}
const successPage=props=>{
firebase.auth().onAuthStateChanged((用户)=>{
如果(用户){
log(“调用成功页面:“+user.uid”)
//检查用户是否为订阅付费
firestore.collection('customers').doc(user.uid).collection('subscriptions'))
.where('status','in',['trialing','active'])。get()
。然后(activeSubscriptions=>{
//如果这是真的,则用户没有活动订阅。
if(activeSubscriptions.empty==true){
console.log(user.uid)
firestore.collection('customers').doc(user.uid)
.get()。然后(
doc=>{
如果(文件存在){
firestore.collection('customers').doc(user.uid).collection('subscriptions').get()。
然后(sub=>{
如果(子文档长度>0){
var activityStatus=“已取消”
createCheckoutSession(活动状态)
log(“存在子集合”);
}否则{
警报(“您的帐户已创建,但您的付款详细信息尚未成功创建。您现在将被重定向到签出页面”)
createCheckoutSession()
console.log(user.uid)
console.log(“不存在!”)
}
});
}
});
}else if(activeSubscriptions.size>1){
警报(“您有多个活动订阅。请管理您的订阅并取消其中一个订阅以访问应用程序”)
}否则{
firestore.collection(“profiledata”).doc(user.uid).update({
帐户状态:“活动”
}).然后(()=>{
火库
.收集roger@x.ca")
.添加({
致:user.email,
信息:{
},
})
。然后(()=>console.log(“发送电子邮件!”);
props.history.push(“/clients”)
})
}
});
}
})
返回(
)
}

它检查status=所在的订阅集合以进行试用或活动,然后检查订阅内的所有内容以查看发生了什么,但由于某种原因,即使已创建订阅集合,它仍会继续重定向到条带页面(createCheckoutSession)。这是时间问题吗?

创建新订阅时,Stripe会触发服务器/云功能的Webhook,之后在Firestore中创建文档。此过程可能需要一些时间,同时您的用户可能已重定向到成功页面。如果尚未创建文档,则无法显示交易状态

以下是您可以执行的解决方法:

  • 在服务器上创建条带签出会话时,您实际上可以创建订阅文档,但可以将名为“Stripe\u response”的字段设置为false,还可以在条带成功\u url中添加新的订阅文档ID作为查询参数。所以您的url可能看起来像:
    https://domain.ext/paymentSuccess?id=
  • 现在,当用户在success页面上时,使用查询参数中提到的ID查找特定的订阅文档。如果“stripe_response”仍然是假的,那么可能webhook还没有完成它的工作。只需在5秒钟后重试请求,然后向用户显示状态。确保在webhook中将
    stripe\u响应设置为true
  • 为了简化步骤2,您只需在文档上附加一个标记,以便在更新状态时尽快得到响应,而不需要依赖轮询

    关于“持续重定向”部分,您能否指定具体的重定向情况?它在哪里重定向和所有这些

    这肯定是一个比赛条件,但如果你按照上面的步骤,它应该照顾它。如果您需要更多帮助来解决此问题,请告诉我