简体   繁体   English

如何在 nodejs 中使用 nexmo 发送短信?

[英]How to work out with sending sms using nexmo in nodejs?

This is the route code:这是路由代码:

router.get('/compose/:ID', function(req, res, next) {
var random = Math.floor((Math.random() * 1000000) + 1);
console.log("random = " + random);
   res.render('compose', { random: random, Id:req.params.ID });
})
.post(function(req, res, next) {

var to = '917985754084';
var from = 'GrumpyText';
var text = req.body.OTP;

console.log(text);

nexmo.message.sendSms(from, to, text, function(err,success){
  if(success)
  console.log("SMS sent successfully!");
  else {
    console.log("error");
  }
});
});

This is the handlebar code:这是车把代码:

<main role="main" class="container">
  <div class="jumbotron">
    <h1>List of Contacts</h1>
    <table class="table table-striped">
  <thead>
    <tr>
      <th scope="col"></th>
      <th scope="col"></th>
    </tr>
  </thead>
  <tbody>
    <form method="post"  enctype="multipart/form-data">
    <tr>
      <td><input type="text" name="OTP" class="form-control" value="Hi. Your OTP is : {{random}}"></td>
      <td></td>
    </tr>
    <tr>
      <td><button type="submit" class="btn btn-lg btn-primary btn-block">Submit</button>
</td>
    </tr>
  </form>
  </tbody>
</table>
  </div>
</main>

Its coming to the route but the console is not printing the 'text'.它到达路线但控制台未打印“文本”。 aka console.log(text) means req.body.OTP is not printing anything.又名console.log(text)意味着req.body.OTP没有打印任何东西。 Earlier it was printing undefined.早些时候它正在打印未定义的。 Could you please rectify where it is stucking perhaps?你能纠正它卡住的地方吗?

The issue you are hitting is body-parser doesn't handle multipart/form-data .您遇到的问题是body-parser不处理multipart/form-data

There are several options, but one of those is multer .有多种选择,但其中之一是multer

\* add these for multi-part*\
var multer  = require('multer')
var upload = multer();
\* other code *\
router
  .get('/compose/:ID', function (req, res, next) {
    var random = Math.floor(Math.random() * 1000000 + 1);
    console.log('random = ' + random);
    res.render('compose', { random: random, Id: req.params.ID });
  })
  .post('/compose/:ID', upload.none(), function (req, res, next) {
    var to = '917985754084';
    var from = 'GrumpyText';
    var text = req.body.OTP;

    nexmo.message.sendSms(from, to, text, function (err, success) {
      if (success) console.log('SMS sent successfully!');
      else {
        console.log('error');
      }
    });
  });

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

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