В настоящее время я учусь создавать чат-бота, который может сообщать погоду (используя nodejs и dialogflow), но я столкнулся с проблемой, для которой не могу найти решения (я новичок в javascript).Каждый раз, когда я пытаюсь запустить свой webhook с помощью dialogflow, я получаю сообщение об ошибке: «TypeError: Невозможно прочитать свойство 'action' of undefined".По-видимому, это происходит от "var action = req.body.result.action;".Вот фрагмент кода:
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const owmToken = ; //Token
const webhook = express();
webhook.set('port',9000); webhook.use(bodyParser.json());
webhook.listen(webhook.get('port'),function() {console.log('webhook démarré en',webhook.get('port'));});
webhook.post('/webhook',function(req,res){
var action = req.body.result.action;
switch(action) {
case 'interrogation_openweathermap': {
var ville=
req.body.result.parameters['ville'];
request.get(
'http://api.openweathermap.com/data/2.5/?q='+ville+'&lang=fr&APPID='+owmToken,
function(error,response,body){
console.log(body);
var json = JSON.parse(body);
out = construireReponseMeteoDuJour(json,"Aujourd'hui", ville);
res.json(out);});
}break ;}
})
Я использую NodeJs 8.11.2
Полное сообщение об ошибке:
{ responseId: '4f56ae2e-3415-4fd3-bcb0-57c76d5b9927',
queryResult:
{ queryText: 'météo nancy',
action: 'interrogation_openweathermap',
parameters: { Ville: 'Nancy' },
allRequiredParamsPresent: true,
fulfillmentText: 'Désolé, mais je ne parviens pas à obtenir la météo de Nancy, veuillez réessayer plus tard !',
fulfillmentMessages: [ [Object] ],
intent:
{ name: 'projects/agentfr-d7830/agent/intents/d37c07f1-b639-48b3-b48a-f9f370843e6d',
displayName: 'Demander_Météo' },
intentDetectionConfidence: 0.89,
diagnosticInfo: {},
languageCode: 'fr' },
originalDetectIntentRequest: { payload: {} },
session: 'projects/agentfr-d7830/agent/sessions/104fd141-0b0a-37cf-2dd0-dbacc9552968' }
> TypeError: Cannot read property 'action' of undefined
> at C:\Users\Asus\Desktop\webhook\index.js:18:30
> at Layer.handle [as handle_request] (C:\Users\Asus\Desktop\webhook\node_modules\express\lib\router\layer.js:95:5)
> at next (C:\Users\Asus\Desktop\webhook\node_modules\express\lib\router\route.js:137:13)
> at Route.dispatch (C:\Users\Asus\Desktop\webhook\node_modules\express\lib\router\route.js:112:3)
> at Layer.handle [as handle_request] (C:\Users\Asus\Desktop\webhook\node_modules\express\lib\router\layer.js:95:5)
> at C:\Users\Asus\Desktop\webhook\node_modules\express\lib\router\index.js:281:22
> at Function.process_params (C:\Users\Asus\Desktop\webhook\node_modules\express\lib\router\index.js:335:12)
> at next (C:\Users\Asus\Desktop\webhook\node_modules\express\lib\router\index.js:275:10)
> at C:\Users\Asus\Desktop\webhook\node_modules\body-parser\lib\read.js:130:5
> at invokeCallback (C:\Users\Asus\Desktop\webhook\node_modules\raw-body\index.js:224:16)
Спасибо за совет,