使用NodeJS和Angular发送邮件

时间:2016-07-17 13:07:16

标签: javascript angularjs node.js

所以我创建了一个发送邮件的表单。它是这样的:

exports.sale = {
  tags: ['api'],
  validate: {
    //blah blah blah
  },
  handler: function(request, reply) {
    var output = {
      success: true,
      operations: [],
      epoch: Date.now()
    };

    Unit.findById(request.payload.deviceNumber, function(err, device) {
      if (err) {
        //blah blah blah
      }
      if (device) {
        Owner.findOne({
          OwnerId: device.Owner
        }, function(err, Owner) {
          if (err) {
            //blah blah blah
          } else if (Owner) {
            //make changes to output.operations
          }
          reply(output);
        });
      } else {
        output.success = false;
        reply(output);
      }
    });
  }
};

ng-click发送功能在控制器中如下所示:

<div class="col-xs-6 col-md-6 form-group">
    <input class=" form-control " requried style="border-radius:20px" ng-model="name" name="name" type="text"  />
</div>
<div class="col-xs-6 col-md-6 form-group">
    <input class="form-control border-radius" required style="border-radius:20px" id="email" name="email" type="email" ng-model="email"  />
</div>
</div>
<textarea class="form-control" id="message" style="border-radius:20px" name="message"  rows="5" ng-model="message"></textarea>
<br />
<div class="row">
    <div  class="col-xs-12 col-md-12 form-group">
    <button ng-click="send(ngCart.getCart().items,message,email,name)" id="send_email" class="btn btn-primary pull-right btn-color" type="submit" style="border-radius:20px">send</button>
    </div>
</div>

在NodeJS中我有以下内容:

  $scope.send = function (items, message, email, name) {

            if (email == undefined) {
                alert("Error!")
            }
            else
            {
                var data = email; 
                var config = {
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
                    }
                }

                $http.post('/send', data, config)
                    .success(function (data, status, headers, config) {
                        $scope.PostDataResponse = data;
                        console.log("Is working", data, status, headers, config);
                    })
                    .error(function (data, status, header, config) {
                        $scope.ResponseDetails = "Data: " + data +
                            "<hr />status: " + status +
                            "<hr />headers: " + header +
                            "<hr />config: " + config;
                    });
            }


        };
    }])

现在问题出在NodeJS中我无法在req或res中看到我想要在角度函数(ngCart.getCart()。项目,消息,电子邮件,名称)中发送的数据,为什么会这样?

编辑: 它在访问localhost / send时会发送带有固定数据的邮件,但它无法从send函数中获取数据,我只是无法弄清楚原因。

1 个答案:

答案 0 :(得分:0)

您的请求有效负载位于req.body