Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法读取属性';行动';未定义的_Javascript_Node.js_Dialogflow Es - Fatal编程技术网

Javascript 无法读取属性';行动';未定义的

Javascript 无法读取属性';行动';未定义的,javascript,node.js,dialogflow-es,Javascript,Node.js,Dialogflow Es,我目前正在学习创建一个聊天机器人,它可以提供天气信息(使用nodejs和dialogflow),但我面临着一个无法找到解决方案的问题(我是javascript新手)。每次尝试使用dialogflow运行webhook时,我都会遇到一个错误:“TypeError:无法读取未定义的属性'action'。显然,它来自“var action=req.body.result.action;”。下面是代码的摘录: const express = require('express'); const bodyP

我目前正在学习创建一个聊天机器人,它可以提供天气信息(使用nodejs和dialogflow),但我面临着一个无法找到解决方案的问题(我是javascript新手)。每次尝试使用dialogflow运行webhook时,我都会遇到一个错误:“TypeError:无法读取未定义的属性'action'。显然,它来自“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 ;}

    })
我正在使用nodejs8.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)

谢谢您的建议,

尝试打印“请求”。由于没有body属性,因此req.body未定义。不幸的是,它没有像其他语言那样给出一个没有属性的错误

正如我在评论中提到的,您没有为urlencoded添加主体解析器

webhook.use(bodyParser.urlencoded({ extended: false }));
为了

请求体结果参数['ville']

你可以用

req.body.queryResult.parameters.VILE


可能您忘了添加express,您应该在创建服务器和设置middlewareprint req.body的地方发布代码主文件,并检查结构。@ArifKhan-将其作为答案发布,因为它看起来是正确的,您可以在回调中打印标题以查看请求内容类型,类似于
console.loge(req.get('Content-Type'));之前
var action=req.body.result.action,您应该在控制台数据上打印
console.log(req.body)
我刚刚做了测试,但结果是一样的。但是现在,我可以用cmdI看到API响应。你能在我的第一条消息中将你能在cmdI上看到的响应粘贴过去吗?我尝试了一下,但得到了相同的结果:(我可以尝试类似queryParam或queryParameters的东西吗?你注意到“Ville”上的大写字母了吗?+您必须使用req.body.queryResult.Villey您的响应数据都包含在一个名为“queryResult”的对象中,因此每当您需要其中的内容时,您都必须使用“req.body.queryResult.sthing”