Reputation: 50
I'm writing an action from an API.AI intent which parses the request and extracts the origin and the destination from the request. The problem is it's throwing a Reference error and showing that the data is undefined.
ReferenceError: orginalData is not defined
at exports.distanceCalculator.functions.https.onRequest (/user_code/index.js:16:43)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:26:47)
at /var/tmp/worker/worker.js:652:7
at /var/tmp/worker/worker.js:636:9
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
index.js
'use strict';
process.env.DEBUG = 'actions-on-google:*';
const App = require('actions-on-google').ApiAiApp;
const functions = require('firebase-functions');
exports.distanceCalculator=functions.https.onRequest((request,response) =>{
const app = new App({request, response});
console.log('Request headers: ' + JSON.stringify(request.headers));
console.log('Request body: ' + JSON.stringify(request.body));
var reJson,distance,duration,resText,reSon;
var request=require('request');
var orginalData=JSON.parse(JSON.stringify(request.body || null);
console.log("The data received is: "+originalData);
var origin=orginalData.result.parameters.geo_city;
var destination=orginalData.result.parameters.geo_city;
var travelMode='driving';
var reqURL="https://maps.googleapis.com/maps/api/distancematrix/json?origins="+origin+"&destinations="+destination+"&mode="+travelMode;
//Logger
console.log('Origin: '+origin);
console.log('Destination: '+destination);
console.log('Travel Mode: '+travelMode);
console.log('Request Url: '+reqURL);
function disCal(app){
//Google Maps API call
request(reqURL,function(error,response,body){
reJson=JSON.parse(body);
distance=reJson.rows[0].elements[0].distance.text;
duration=reJson.rows[0].elements[0].duration.text;
resText="The distance between "+origin+" and "+destination+" is "+distance+" and it would take you "+duration+" to reach there"
});
console.log(global.resText);
global.reSon=JSON.parse('{"displayText": global.resText}');
app.post('/webhook', (req, res) =>
{
var reSon=JSON.parse('{"displayText": global.resText}');
return res.json(reSon);
});
app.tell(reSon);
return res.json(reSon);
}
let actionMap=new Map();
actionMap.set('cities.distance',disCal);
app.handleRequest(actionMap);
});
Upvotes: 1
Views: 2738
Reputation: 111506
The error message tells you everything - what's wrong and the exact line.
Note that:
orginalData !== originalData
A good linter like ESLint would help you avoid mistakes like that. See:
Following a good style guide would also help you a lot. For example:
Upvotes: 1