1

I have a working RESTful API, tested with Postman and also the GET port works within my app as well.

However when it comes to inserting it fails with the following error in the console:

Error: undefined is not an object (evaluating 'newrecipeService.create')

I use the following factory for the create action:

app.factory('newrecipeService', function ($resource) {
  return $resource('/api/recipes/', {}, {
   create: {method: 'POST'}
  });
});

And the following controller:

app.controller('FormController', ['$scope',function($scope,newrecipeService){
  $scope.addRecipe = function() {
    newrecipeService.create({name: 'Test', nameID: 'test', category: 'TestCat', categoryID: 'testcat'});
  };
}]);

I pass the controller to my view with a route. I call the addRecipe action with a button and ng-click:

ng-click="addRecipe()

Could you please help me out on this?

1 Answer 1

1

Basically you had missed to inject newrecipeService in your controller function, so in your case newrecipeService is undefined. You must have getting error in console for this case.

app.controller('FormController', ['$scope',function($scope,newrecipeService){

should be changed to

app.controller('FormController', ['$scope', 'newrecipeService' ,
    function($scope,newrecipeService){

Edit

For getting data on server side you need to change the object properties to get them up on server side by matching properties name.

app.controller('FormController', ['$scope',function($scope,newrecipeService){
  $scope.addRecipe = function() {
    newrecipeService.create({
       newRecN‌​ame: 'Test', 
       newRecNameID: 'test', 
       newRecCategory: 'TestCat', 
       newRecIngredients: 'someIngredients',
       newRecMethod: 'someRecMethod'
    });
  };
}]);
Sign up to request clarification or add additional context in comments.

7 Comments

That sorted the problem with the console error. I am accepting your answer. Thank you. However, it still does not post into my database.. As I said, the API is good, I am making the mistake in the front-end bit. Any idea with this one?
@BenS does your all the properties on server side & client side are matching there name like name can be Name on the server side..could you double check that..
These are the columns in mysql: cl.ly/3C451a1b3B2c And this is the REST.js : so in rest I usethese pairs: "name", req.body.newRecName and etc respectively..
@BenS by looking at the column name..it looks ok..but still thinking somewhere its messing.. isn't you have model for DB objects on server side?
I am not sure what do you mean by model for DB objects, but here is the related REST api: ``` router.post("/recipes",function(req,res){ var query = "INSERT INTO recipes (??,??,??,??,??,??) VALUES (?,?,?,?,?,?)"; var table = ["name","nameID","category","categoryID","ingredients","method",req.body.newRecName,req.body.newRecNameID,req.body.newRecCategory,req.body.newRecCategoryID,req.body.newRecIngredients,req.body.newRecMethod]; query = mysql.format(query,table); connection.query(query,function(err,rows){ ```
|

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.