繁体   English   中英

类型错误:无法读取未定义的属性“数字”

[英]TypeError: Cannot read property 'Digits' of undefined

我在 Node.js Twilio 文档中使用通过键盘收集用户输入(DTMF 音调)中的代码来从通话中获取用户输入。

但每次我拨入号码时,我都会收到一个错误:“类型错误:无法读取未定义的属性‘数字’”

谢谢!

代码:

app.post('/voice', (request, response) => {
   const twiml = new VoiceResponse();

   function gather() {
     const gatherNode = twiml.gather({ numDigits: 1 });
     gatherNode.say('For sales, press 1. For support, press 2.');
     twiml.redirect('/voice');
   }
   if (request.body.Digits) {
     switch (request.body.Digits) {
     case '1':
       twiml.say('You selected sales. Good for you!');
       break;
     case '2':
       twiml.say('You need support. We will help!');
       break;
     default:
       twiml.say("Sorry, I don't understand that choice.").pause();
       gather();
       break;
    }
  } else {
gather();
}

response.type('text/xml');
response.send(twiml.toString());
});

初始化Express后是否添加了以下内容?

// Body Parser Middleware
app.use(express.urlencoded({ extended: false }));

就像是:

const express = require('express')

const app = express()

app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded

app.post('/profile', function (req, res, next) {
  console.log(req.body)
  res.json(req.body)
})

看起来文档中有它,所以它可能在您的代码中被意外遗漏了?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM