如何在回叫中实施Firebase设置请求 [英] How can I implement a firebase set request within a call back

查看:66
本文介绍了如何在回叫中实施Firebase设置请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Node js的新手,我想在回调函数内将信息写入Firebase数据库.

I'm new to node js and I would like to write information within a callback function to my firebase database.

我一直在搜索,似乎回调是异步的.如何在此回调中使用firestore?

I've been searching and it seems that the callback is asynchronous. How can I use firestore in this callback?

    exports.registerRestaurantPayout = functions.firestore.document('*********')
  .onCreate(async (paymentSnap, context) => {

    var request = require('request');

    var authCode = paymentSnap.data().auth_code;
    var firstString = 'client_secret=********&code=';
    var secondString = '&grant_type=authorization_code';
    var dataString = firstString + authCode + secondString;

    var options = {
        url: 'https://connect.stripe.com/oauth/token',
        method: 'POST',
        body: dataString
    };

    function callback(error, response, body) {
        if (!error && response.statusCode === 200) {
            console.log(body);

            return await firestore.document('***********')
              .set({'account': body}, {merge: true});
            //return await paymentSnap.ref.set({'account': body}, {merge: true});
        }else{


            //return await paymentSnap.ref.set({'error' : error}, { merge: true });
        }
    }

    request(options, callback);

  });

我收到以下错误解析错误:即使我可以在回调外部使用firestore,也出现了意外的令牌firestore.具体的问题是回调中的return语句

I get the following error Parsing error: Unexpected token firestore even though I can use firestore outside of the callback. The specific problem is the return statement in the callback

推荐答案

在Cloud Function中,您应该使用promise处理异步任务(例如对Stripe API的HTTP调用或对实时数据库的写入).默认情况下,request不返回承诺,因此您需要使用接口包装程序进行请求,例如 ,并根据以下内容修改代码:

In a Cloud Function you should use promises to handle asynchronous tasks (like the HTTP call to the stripe API, or the write to the Realtime Database). By default request does not return promises, so you need to use an interface wrapper for request, like request-promise, and adapt your code along the following lines:

const rp = require('request-promise');

exports.registerRestaurantPayout = functions.firestore.document('*********')
 .onCreate((paymentSnap, context) => {

   var authCode = paymentSnap.data().auth_code;
   var firstString = 'client_secret=**********=';
   var secondString = '&grant_type=authorization_code';
   var dataString = firstString + authCode + secondString;


   var options = { 
     method: 'POST',
     uri: 'https://connect.stripe.com/oauth/token',
     body: dataString,
     json: true // Automatically stringifies the body to JSON
   };

   return rp(options)
   .then(parsedBody => {
       return paymentSnap.ref.set({'account': parsedBody}, {merge: true});
   })
   .catch(err => {
       return paymentSnap.ref.set({'error' : err}, { merge: true });
   });

});


我还建议您观看以下来自Firebase团队的必看"视频,内容涉及云功能和承诺: https://www.youtube.com/watch?v=7IkUgCLr5oA https://www.youtube.com/watch?v=652XeeKNHSk .


I would also suggest that you watch the two following "must see" videos from the Firebase team, about Cloud Functions and promises: https://www.youtube.com/watch?v=7IkUgCLr5oA and https://www.youtube.com/watch?v=652XeeKNHSk.

这篇关于如何在回叫中实施Firebase设置请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆