【问题标题】:The data alone is missing when I try to send from Angular.js to Node.js当我尝试从 Angular.js 发送到 Node.js 时,单独的数据丢失了
【发布时间】:2017-01-05 05:35:06
【问题描述】:

我正在尝试将数据从我的Angular.js 控制器发送到Node.js 后端。提出请求时,我成功创建了MongoDB 条目。但 MongoDB 条目中缺少数据。我被卡住了,无法再继续使用我的应用程序了。谁能给我一个清楚的解释为什么我无法将表单数据发送到Node.js

我已将数据架构放在这里:

var common = require('../common');

var inviteeSchema = common.Schema({
    email: String
}); 
var Invite = common.conn.model('Invite', inviteeSchema);
module.exports = Invite;

我在这里附上了路由代码。

router.route('/addtoinvitelist').post(function (req, res, next) {

var invite =new Invite(req.body);

invite.save(function(err,email){
   if(err) throw err;

    res.json({message:"your mail id is stored in our database we will soon send you an invite"})
});
});

我的 HTML 表单放在这里

<form action="#">
                                            <div class="form-group">
                                                <label for="subcribeNewsletter" class="control-label">INVITE FORM<br> <small>We are happy to invite you to medicoshere, So please enter your email ID in the below form.</small></label>
                                                <div class="input-group input-group-in input-group-sm">
                                                    <div class="input-group-addon"><i class="fa fa-envelope text-belpet"></i></div>
                                                    <input class="form-control" id="subcribeNewsletter" placeholder="name@mail.com" ng-model="useremail" required>
                                                    <div class="input-group-btn">
                                                        <button type="submit" class="btn btn-default text-belpet" ng-click="AddToInviteList(useremail)"><strong>OK</strong></button>
                                                    </div>
                                                </div>
                                            </div><!-- /.form-group -->
                                        </form><!-- /form -->

我的角度服务功能在这里 `this.AddToInviteList = function (email, cb) {

    $http({

        method: 'POST',
        url: "http://localhost:3000/users/addtoinvitelist",
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }, // set the headers so angular passing info as form data (not request payload)
        data:"email"

    }).success(function (data) {

        console.log("email is posted sucessfully" + data);
        cb(data);
    })

}`

控制器功能在这里

App.controller('InviteController', function ($scope, $rootScope, $routeParams, $location, UserServices) {
$scope.init = function () {
    console.log("hii this is a test ")
};


$scope.email = {};

$scope.AddToInviteList = function () {

    UserServices.AddToInviteList($scope.email, function (dataresponse) {


        console.log(dataresponse);

    })

}

});

【问题讨论】:

  • 您是否在后端获得email 值?
  • 是的,数据库中有一个空字符串...
  • 因为您将静态email 作为字符串发送,所以在正文中使用data:email
  • 如果我提供数据:电子邮件,那么数据库中没有电子邮件条目,只有对象 ID 进入数据库...
  • 您的模型名称是“useremail”,但您将“$scope.email”传递给服务。

标签: angularjs node.js mongodb http multipartform-data


【解决方案1】:

将电子邮件作为 json 对象 { 'email' : email } 传递。

试试这个代码:

 $http({
    method: 'POST',
    url: "http://localhost:3000/users/addtoinvitelist",
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    data:{ 'email' : email }

  }).success(function (data) {

    console.log("email is posted sucessfully" + data);
    cb(data);
  })

控制器:

App.controller('InviteController', function ($scope, $rootScope, $routeParams, $location, UserServices) {
$scope.init = function () {
    console.log("hii this is a test ")
};

$scope.AddToInviteList = function () {
    $scope.user = {
       email : $scope.useremail
    };

    UserServices.AddToInviteList($scope.user, function (dataresponse) {
      console.log(dataresponse);
    })
   }
});

在服务器端,您可以通过调用“req.body.email”访问电子邮件

【讨论】:

    【解决方案2】:

    HTML input element 中的model 名称更改为email 并将request 发送为

    $http({
        method: 'POST',
        url: "http://localhost:3000/users/addtoinvitelist",
        headers: {
            'Content-Type': 'application/json'
        },
        data:{ 'email' : email }
    
      }).success(function (data) {
    
        console.log("email is posted sucessfully" + data);
        cb(data);
      })
    

    并在后端获取它

    req.body.email

    我认为这应该可行!

    更新

    App.controller('InviteController', function ($scope, $rootScope, $routeParams, $location, UserServices) {
    $scope.init = function () {
        console.log("hii this is a test ")
    };
    
    
    $scope.AddToInviteList = function () {
    
        UserServices.AddToInviteList($scope.email, function (dataresponse) {
    
    
            console.log(dataresponse);
    
        })
    
    }
    

    【讨论】:

    • 你把ng-model="useremail"改成ng-model="email"了吗?
    • 是的,你建议的所有事情都做了,但它变得未定义.....不知道为什么....我坚持了..
    • 您的角度控制器中的email 值是否正确?
    • 没有我在哪里得到电子邮件值....都是未定义的......角度和节点都说它的未定义......!
    • 从控制器中删除$scope.email = {}; 这一行,然后打印console.log($scope.email)。如果你得到了,那么我认为它一定是工作
    猜你喜欢
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 2018-06-22
    • 2014-07-18
    • 2019-10-09
    • 2016-03-22
    • 2023-03-13
    • 2021-09-27
    相关资源
    最近更新 更多