我正在使用SailsJS和Angular将注册表单的数据发布到MongoDB。我可以成功获取表单数据以登录到控制台,但它不会POST到数据库。
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
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.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 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>
我究竟如何在Sails中获取表单数据到POST数据库?
答案 0 :(得分:0)
您的风帆应用中有这个自定义操作路线:/User/create
在角度控制器中,您需要输入正确的路线:$http.post('/User/create', params)
更多信息:http://sailsjs.org/documentation/reference/blueprint-api#?action-routes