ReferenceError:数据未在exports.action中定义

Posted

技术标签:

【中文标题】ReferenceError:数据未在exports.action中定义【英文标题】:ReferenceError:data is not defined in at exports.action 【发布时间】:2017-12-15 07:59:53 【问题描述】:

我正在从 API.AI 意图中编写一个动作,该意图解析请求并从请求中提取来源和目的地。 问题是它抛出一个引用错误并显示数据未定义。

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

【问题讨论】:

【参考方案1】:

错误消息会告诉您一切 - 出了什么问题以及确切的行。

注意:

orginalData !== originalData

像 ESLint 这样好的 linter 可以帮助你避免这样的错误。见:

http://eslint.org/

遵循良好的风格指南也会对您有很大帮助。例如:

http://airbnb.io/javascript/

【讨论】:

以上是关于ReferenceError:数据未在exports.action中定义的主要内容,如果未能解决你的问题,请参考以下文章

ReferenceError 'model' 未在 mongoose 中定义

ReferenceError: primordials 未在 node.js 中定义

ReferenceError:FlatListItemSeparator 未在 React Native 中定义

UnhandledPromiseRejectionWarning:ReferenceError:TextEncoder 未在 NextJS 12 中定义

UnhandledPromiseRejectionWarning:ReferenceError:TextEncoder 未在 NextJS 12 中定义

未捕获的 ReferenceError:RegeneratorRuntime 未在 React 中定义