调用函数后数据返回null [英] Data returned null after calling function

查看:84
本文介绍了调用函数后数据返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试做什么:

将付款订单放置在付款网关的服务器上(使用Firebase功能).

placeorder()创建订单(此部分有效)

the placeorder() creates the order (this part works)

我想要下订单的数据的详细信息,而 function(err,data)具有该数据.但每次返回null.

I want the details of the placed order's data back, and the function(err,data) has that data. but everytime it returns null.

我尝试过的事情:

非常确信这是基于异步执行的问题,尝试对其进行纠正,并且此代码是它的结果,但数据仍以 NULL 返回

was pretty convinced that it was a problem based on asynchronous execution, tried rectifying it and this code is the result of it , still the data is being returned as NULL

 const functions = require("firebase-functions");

 
/* eslint-disable */

exports.order = functions.https.onCall( (amnt, context) => {
    
    var orderdata;
    const Ippopay = require('node-ippopay');
    var ippopay_instance = new Ippopay({
        public_key: 'pk_live_0WZhCNC5l7PJ',
        secret_key: 'my secret key',
      });
      
       async function placeorder(){
        ippopay_instance.createOrder({
            amount: amnt,
            currency: 'INR',
            payment_modes: "cc,dc,nb,upi",
            customer: {
                name: "Test",
                email: "test@gmail.com",
                phone: {
                    country_code: "91",
                    national_number: "9876543210"
                }
            }
        },
  
            function (err, data) {
  
                if (err) {
                    console.log(err);
                    return;
                }
                orderdata= JSON.parse(data);
  
          }) ;
      }  
        async function orderdatareturn() {
            console.log('before promise call')
                  // Await for the placeorderdata() to complete
            let result =await placeorder();
            return result; 
        }; 
        return orderdatareturn();
}); 

推荐答案

我要做的第一件事是"promisify".ippopay api的回调样式入口点...

The first thing I'd do is "promisify" the callback-style entry point for the ippopay api...

const Ippopay = require('node-ippopay');
const ippopay_instance = new Ippopay({
  public_key: 'public key',
  secret_key: 'secret key',
});


// call ippopay api to createOrder and resolve with the JSON result parsed
async function placeorder(params) {
  return new Promise((resolve, reject) => {
    ippopay_instance.createOrder(params, (error, data) => {
      if (error) reject(error);
      else resolve(JSON.parse(data));
    })
  });
}

现在,导出的函数可以变得简单明了.关键是要在返回之前等待承诺解决方案...

Now the exported function can be made simple and clear. The key is to await the promise resolution before returning...

exports.order = functions.https.onCall((amnt, context) => {
  const params = { amount: amnt, etc...}
  return await placeorder(params);
})

这篇关于调用函数后数据返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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