TypeError: Cannot read property 'Digits' of undefined

TypeError: Cannot read property 'Digits' of undefined

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

但每次我拨入号码时都会收到错误消息:“类型错误:无法读取未定义的 属性 'Digits'”

谢谢!

代码:

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

看起来 documentation 有它,所以可能是您的代码中不小心遗漏了它?