简体   繁体   中英

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. 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 .

There are several options, but one of those is 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');
      }
    });
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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