Monday, 25 April 2022

http request does not get called when uploading the data to Firebase - firebase cloud function

Hello everyone I am trying to fetch the data from Binance than do a schedule function that uploads the data from the previous fetch to the Cloud FireStore , the schedule function is a function that gets called every minute and MUST call the fetch function update the object that contain the data and than I parse that object in order to upload the updated data.

The problem is that the function that fetches the data gets called just one time and that is it, the next minute after the function deploy the fetch data gets not called and I tried everything I found on stackoverflow and google but not luck.

Can anyone give me a hint thank you.

Code:

"use strict";
Object.defineProperty(exports, "__esModule", {value: true});
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const https = require("https");
admin.initializeApp();
const database = admin.firestore();
let finalData = [];
const page = 1;
const fiat = "RON";
const tradeType = "SELL";
const asset = "USDT";
const payTypes = ["BCRBank"];
const baseObj = {
  page,
  rows: 20,
  publisherType: null,
  asset,
  tradeType,
  fiat,
  payTypes,
};
const stringData = JSON.stringify(baseObj);
const options = {
  hostname: "p2p.binance.com",
  port: 443,
  path: "/bapi/c2c/v2/friendly/c2c/adv/search",
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "Content-Length": stringData.length,
  },
};

finalData = [];
const req = https.request(options, (res) => {
  console.log("Entered the request call");
  let output = "";
  res.on("data", (d) => {
    output += d;
  });
  res.on("end", () => {
    try {
      const jsonOuput = JSON.parse(output);
      const allData = jsonOuput["data"];
      for (let i = 0; i < allData.length; i++) {
        let payTypesz = "";
        for (let y = 0; y < allData[i]["adv"]["tradeMethods"].length; y++) {
          payTypesz += allData[i]["adv"]["tradeMethods"][y]["payType"];
          if (y < allData[i]["adv"]["tradeMethods"].length - 1) {
            payTypesz += ", ";
          }
        }
        const obj = {
          tradeType: allData[i]["adv"]["tradeType"],
          asset: allData[i]["adv"]["asset"],
          fiatUnit: allData[i]["adv"]["fiatUnit"],
          price: allData[i]["adv"]["price"],
          surplusAmount: allData[i]["adv"]["surplusAmount"],
          maxSingleTransAmount: allData[i]["adv"]["maxSingleTransAmount"],
          minSingleTransAmount: allData[i]["adv"]["minSingleTransAmount"],
          nickName: allData[i]["advertiser"]["nickName"],
          monthOrderCount: allData[i]["advertiser"]["monthOrderCount"],
          monthFinishRate: allData[i]["advertiser"]["monthFinishRate"],
          advConfirmTime: allData[i]["advertiser"]["advConfirmTime"],
          payTypes: payTypesz,
          position: 0,
        };
        finalData.push(obj);
      }
      console.log(finalData);
    } catch (e) {
      console.log(e);
    }
  });
});

const fD =function fetchData(stringData) {
  console.log("I entered the FetchData");
  req.write(stringData);
};

exports.scheduledFunction = functions.pubsub
    .schedule("* * * * *")
    .onRun(async (context) => {
      console.log("I am doing it");
      fD(stringData);
      for (let i = 0; i < finalData.length; i++) {
        console.log("I did not entered here");
        console.log(finalData[i]["price"]);
        await database.collection("SebiBinanceSale").doc("BCR Bank")
            .collection("1").doc(i.toString())
            .set({
              "tradeType": finalData[i]["tradeType"],
              "asset": finalData[i]["asset"],
              "fiatUnit": finalData[i]["fiatUnit"],
              "price": finalData[i]["price"],
              "surplusAmount": finalData[i]["surplusAmount"],
              "maxSingleTransAmount": finalData[i]["maxSingleTransAmount"],
              "minSingleTransAmount": finalData[i]["minSingleTransAmount"],
              "nickName": finalData[i]["nickName"],
              "monthOrderCount": finalData[i]["monthOrderCount"],
              "monthFinishRate": finalData[i]["monthFinishRate"],
              "advConfirmTime": finalData[i]["advConfirmTime"],
              "payTypes": finalData[i]["payTypes"],
              "position": finalData[i]["position"],
            });
      }
      return console.log("Succes Upload of the data ");
    });


from http request does not get called when uploading the data to Firebase - firebase cloud function

No comments:

Post a Comment