JavaScript:另一个函数中的调用函数不起作用

时间:2018-06-03 04:23:09

标签: javascript function dialogflow

我在另一个函数中调用函数时遇到问题。

我有一个函数调用,getAllStockTickers,它返回公司的所有股票代码,“newStockTickerArray”。

我有另一个函数调用getStockPrice,它返回股票的价格。

我试图在“getStockPrice”函数中调用“getAllStockTickers”函数,并使用在“getStockPrice”函数中调用“getAllStockTickers”函数返回的值。

在“getStockPrice”功能中,我做

const stockList = getAllStockTickers(username, password);

console.log(stockList);

但是,我没有为该console.log定义。

这是为什么?我该如何解决这个问题?

'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));
		})
	});
}

0 个答案:

没有答案