1

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>

1 Answer 1

2

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"});
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.