简体   繁体   English

在SailsJS中将Angular表单数据发布到MongoDB

[英]Posting Angular form data to MongoDB in SailsJS

I am using SailsJS and Angular to post a signup form's data to MongoDB. 我正在使用SailsJS和Angular将注册表单的数据发布到MongoDB。 I can successfully GET the form data to log to the console, but it won't POST to the database. 我可以成功获取表单数据以登录到控制台,但不会过帐到数据库。

User.js user.js的

module.exports = {
  connection: 'someMongodbServer',
  tableName: 'test',
  attributes: {
    id: {
      type: 'integer',
      unique: true,
      primaryKey: true
    },
    name: {
      type: 'string',
      required: true
    },
    email: {
      type: 'email',
      unique: true,
      required: true
    },
    password: {
      type: 'string',
      required: true
    },
    address: {
      type: 'string'
    },
    phone: {
      type: 'string'
    },
    created_at: {
      type: 'datetime',
      autoCreatedAt: true
    },
    updated_at: {
      type: 'datetime',
      autoUpdatedAt: true
    },
        toJSON: function() {
            var obj = this.toObject();
            delete obj.password;
            return obj;
        }
  }
};

UserController.js UserController.js

create: function(req, res) {
    var name = req.param('name');
    var email = req.param('email');
    var password = req.param('password');

    console.log(req.params.all());

    User.create({name: name, email: email, password: password}).exec(function(err, user) {
      if (err) {
        // res.send(500, {error: "DB Error"});
        res.json(err.status, {err: err});
        return;
      } else {
        req.session.user = user;
        res.send(user);
      }
    });
  }

Angular controller 角度控制器

angular.module('signup').controller('SignupController', ['$scope', '$state', '$http', function($scope, $state, $http) {
    // $scope.form_signup = {}

    $scope.form_submit_signup = function(req, res) {
      var params = {
        name: $scope.form_signup.name,
        email: $scope.form_signup.email,
        password: $scope.form_signup.password
      }

      $http.post('/signup', params).success(function(){
        console.log("Success!", params);
      }).error(function(err) {
        console.log("Error!", err);
      });
    }
  }]);

Form 形成

<form ng-submit="form_submit_signup()" name="signup">
      <div class="form-group">
        <label for="exampleInputEmail1">Full Name</label>
        <input type="text" class="form-control" name="name" placeholder="Name" ng-model="form_signup.name" required>
      </div>
      <div class="form-group">
        <label for="exampleInputEmail1">Email address</label>
        <input type="email" class="form-control" name="email" placeholder="Email" ng-model="form_signup.email" required>
      </div>
      <div class="form-group">
        <label for="exampleInputPassword1">Password</label>
        <input type="password" class="form-control" name="password" placeholder="Password" ng-model="form_signup.password" required>
      </div>
      <!--
      <div class="form-group">
        <label for="exampleInputPassword1">Confirm Password</label>
        <input type="password" class="form-control" name="password" placeholder="Password" ng-model="form_signup.password">
      </div>
      -->
      <button type="submit" class="btn btn-block btn-lg btn-primary">Signup</button>
    </form>

How exactly would I get the form data to POST to the database in Sails? 我将如何准确地将表单数据发送到Sails中的POST到数据库中?

You have this custom action route in your sails app: /User/create 您在sails应用程序中具有以下自定义操作路线: /User/create

In your angular controller, you need put correct route: $http.post('/User/create', params) 在角度控制器中,您需要输入正确的路径: $http.post('/User/create', params)

More information: http://sailsjs.org/documentation/reference/blueprint-api#?action-routes 详细信息: http : //sailsjs.org/documentation/reference/blueprint-api#?action-routes

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

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