0

I am having an issue calling a function in another function.

I have a function call, getAllStockTickers, which returns all the stock tickers of the companies, "newStockTickerArray".

I have another function call getStockPrice, which returns price of the stock.

I am trying to call "getAllStockTickers" function inside the "getStockPrice" function and use the value that is being returned from calling "getAllStockTickers" function inside the "getStockPrice" function.

In "getStockPrice" function, I do

const stockList = getAllStockTickers(username, password);

console.log(stockList);

However, I get undefined for that console.log.

Why is this? How can I solve this issue?

'use strict';

var https = require('https');
const functions = require('firebase-functions');
const allStockTickers = '';

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
 const action = request.body.queryResult.action;
 response.setHeader('Content-Type', 'application/json');

 if(action != 'input.getStockPrice') {
  response.send(buildChatResponse("I'm sorry, I don't know this"))
  return ;
 }

 const parameters = request.body.queryResult.parameters;
 var companyName = parameters['company_name'];
 var priceType = parameters['price_type'];
 var date = parameters['date'].substring(0,10);

 getStockPrice(companyName, priceType, date, response);
});

function buildChatResponse(chat) {
 return JSON.stringify({ "fulfillmentMessages": [
   {
    "text": {
     "text": [
      chat
     ]
    }
   }
  ] 
 });
};

function getAllStockTickers(username, password) {
 var pathString = "/companies";
 var auth = "Basic " + new Buffer(username + ":" + password).toString('base64');
 const newStockTickerArray = [];

 var request = https.get({
  host: "api.intrinio.com",
  path: pathString,
  headers: {
   "Authorization": auth
  }
 }, function(response) {
  var json = "";

  response.on('data', function(chunk) {
   console.log("Received json response: " + chunk);
   json += chunk;
  });

  response.on('end', function() {
   var jsonData = JSON.parse(json);
   var newJsonData = JSON.stringify(jsonData);
   var stockJsonData = jsonData.data;

   var obj = {};

   for(var i = 0; i < stockJsonData.length; i++) {
    const singleData = stockJsonData[i];
    const ticker = singleData.ticker;
    const companyName = singleData.name;

    obj[companyName] = ticker;
   }

   newStockTickerArray.push(JSON.stringify(obj));

   return newStockTickerArray;
  })
 });
}

function getStockPrice(companyName, priceType, date, cloudFnResponse) {

 var tickerMap = {
  "apple": "APPL",
  "microsoft": "MSFT",
  "ibm": "IBM",
  "google": "GOOG",
  "facebook": "FB",
  "amazon": "AMZN"
 };

 var priceMap = {
  "opening": "open_price",
  "closing": "close_price",
  "maximum": "high_price",
  "high": "high_price",
  "low": "low_price",
  "minimum": "low_price"
 };

 var stockTicker = tickerMap[companyName.toLowerCase()];
 var priceTypeCode = priceMap[priceType.toLowerCase()];

 var pathString = "/historical_data?ticker=" + stockTicker +
 "&item=" + priceTypeCode +
 "&start_date=" + date +
 "&end_date=" + date;

 var username = "123241312";
 var password = "w3qw312312";

 const stockList = getAllStockTickers(username, password);

 console.log(stockList);

 var auth = "Basic " + new Buffer(username + ":" + password).toString('base64');

 var request = https.get({
  host: "api.intrinio.com",
  path: pathString,
  headers: {
   "Authorization": auth
  }
 }, function(response) {
  var json = "";
  response.on('data', function(chunk) {
   json += chunk;
  });

  response.on('end', function() {
   var jsonData = JSON.parse(json);
   var stockPrice = jsonData.data[0].value;
   var chat = "The " + priceType + " price for " + companyName + " on " + date + " was " + stockPrice;

   cloudFnResponse.send(buildChatResponse(chat));
  })
 });
}
Eunicorn
  • 601
  • 6
  • 16
  • 29
  • `getAllStockTickers` doesn't return anything. The `return` statement does not cross function boundaries. `getAllStockTickers` contains **asychronous** code. You need to refactor your code to handle that. See the duplicate for various solutions. – Felix Kling Jun 03 '18 at 04:27
  • @Felix Kling I am still having issue refactoring my code to handle that. Could you please help me on this? – Eunicorn Jun 04 '18 at 02:45

0 Answers0