I am new and a bit confused about using AngularJS to POST form data to the API of sails.js, but the form does not work, with this not-so-informative error: TypeError: string is not a function
I was wondering what might be wrong. I'm suspecting the API I'm using is wrong in the $http.post()
, but I'm not sure.
I have the following Employee
model and EmployeeController
method called regEmployee()
:
Employee Model:
module.exports = {
attributes: {
name:{
type:"string",
required:true,
minLength: 2
},
empnum:{
type:"string",
required:true,
unique: true
},
email:{
type:"email",
required:true,
unique: true
}
}
};
regEmployee() method:
$scope.regEmployee = function() {
$http.post("http://localhost:1337/employee/create", {name: $scope.employee.name,
empnum: $scope.employee.number, email: $scope.employee.email}
).success($scope.message = "successful registration"
).error($scope.message = "failed registration");
}
The form in html:
<form name="newEmp" ng-submit="regEmployee()" novalidate>
<div class="form-group-lg">
<label for="name">Employee Name: </label>
<input type="text" id="name" placeholder="Employee Name" ng-model="employee.name" name="name" ng-required="true">
</div>
<div class="form-group-lg">
<label for="number">Employee Number: </label>
<input type="number" id="number" placeholder="Employee Number" ng-model="employee.number" name="number" ng-required="true">
</div>
<div class="form-group-lg">
<label for="email">Employee Email: </label>
<input type="email" id="email" placeholder="Employee Email" ng-model="employee.email" name="email" ng-required="true">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
The problem is that you are passing strings to your .success and .error functions instead of call back functions. you need something like
$scope.regEmployee = function() { $http.post("http://localhost:1337/employee/create", {name: $scope.employee.name, empnum: $scope.employee.number, email: $scope.employee.email} ).success(function() {$scope.message = "successful registration";}) ).error(function() {$scope.message = "failed registration"}); }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.