Angularjs 通过Twilio POST发送短信时出错

Angularjs 通过Twilio POST发送短信时出错,angularjs,rest,post,sms,twilio,Angularjs,Rest,Post,Sms,Twilio,这是在我的头撞了屏幕半天之后,所以任何帮助都将不胜感激 我正试图通过Twillio发送一条关于点击事件的短信。我使用Angular,在单击时调用SendTestMessage函数。不幸的是,我一直遇到这个错误: POST http://localhost:3000/sendsms 500 (Internal Server Error) 这是我的控制器: .controller('WhotoMessageCtrl', function($scope, UserService, $http,

这是在我的头撞了屏幕半天之后,所以任何帮助都将不胜感激

我正试图通过Twillio发送一条关于点击事件的短信。我使用Angular,在单击时调用SendTestMessage函数。不幸的是,我一直遇到这个错误:

POST http://localhost:3000/sendsms 500 (Internal Server Error)
这是我的控制器:

  .controller('WhotoMessageCtrl', function($scope, UserService, $http, $q){
  console.log("whotomessage page");

  $scope.saveContactInfo = saveContactInfo;
  $scope.contact = {};
  $scope.sendTestMessage = sendTestMessage;

  function sendTestMessage(number){
    console.log('this fired', number);
    var defer = $q.defer();
    $http({
        url: '/sendsms',
        method: 'POST', 
        data: JSON.stringify({number}),
        contentType: 'application/json',
      }).success(function (number){
        console.log('text has been sent to', number);
        defer.resolve(user);
      });
      return defer.promise;
  };
这是我的服务器端代码:

app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'OPTIONS,GET,POST,PUT,DELETE');
    res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
    if ('OPTIONS' == req.method){
        return res.send(200);
    }
    next();
});

app.use(function (req, res, next) {

var sendSMS = function(to){
  var outgoing = {};
  outgoing.to = to;
  outgoing.from = '19725593683';
  outgoing.body = 'Your table is ready';

  client.messages.create(outgoing, function(error, message){
    if (error){console.log(error.message)}
  })
};



app.post('/sendsms', function(req, res){
  sendResponse(res, req.body.phoneNo, 201)
  sendSMS(req.body.phoneNo)
  res.end();
});

有什么建议吗

嘿,这里是Twilio开发者福音传道者

可能是您忘记了复制一些代码,但似乎还没有定义
client
,这意味着您甚至没有向Twilio发出请求

初始化
客户机的正确方法是:

只有这样,您才能在代码上执行以下操作:

var sendSMS = function(to){
  var outgoing = {};
  outgoing.to = to;
  outgoing.from = '19725593683';
  outgoing.body = 'Your table is ready';

  client.messages.create(outgoing, function(error, message){
    if (error){console.log(error.message)}
  })
};

希望这对你有帮助

抱歉,忽略第二个应用。在服务器端代码中使用。谢谢,Marcos!我解决了这个问题,遇到了另一个@我的回答对你有帮助吗?如果是,请将其标记为正确答案,并在此处发布新问题。
var sendSMS = function(to){
  var outgoing = {};
  outgoing.to = to;
  outgoing.from = '19725593683';
  outgoing.body = 'Your table is ready';

  client.messages.create(outgoing, function(error, message){
    if (error){console.log(error.message)}
  })
};