Callback when $save is not working, angular js

I have this save method.

  $scope.save = function(){
    $scope.product.$save(function(pipe){ 
      alert("fire!"); //not working
    });
  };

It works. It save the post to the database. But I want to display a message for the users. Something like "Your update has been saved". So obviously the callback method is suited for this. But it's not working. I get no alert("fire") when I run this. Even though the $save is successful. What can be wrong? How can I debug this?

PS. It's hard do make a fiddle/plunker out of this. But leave comment if I need to include any more code.

Answers


Problem solved.

The issue was that the server (node js) sent an incomplete response.

I only had this:

connection.query("UPDATE product SET ?  WHERE id = ?"

I changed it to this:

  if (connection) {
    connection.query("UPDATE product SET ?  WHERE id = ?",  post, function(err, rows, fields) {
      if (err) throw err;
      res.contentType('application/json');
      res.write(JSON.stringify(rows));
      res.end();
    });
  }

Then the callback worked as expected.


Property Evaluation

Evaluation of all properties takes place against a scope. Unlike JavaScript, where names default to global window properties, Angular expressions have to use $window to refer to the global window object. For example, if you want to call alert(), which is defined on window, in an expression you must use $window.alert(). This is done intentionally to prevent accidental access to the global state (a common source of subtle bugs).

Inject the $window module into your Controller first and then do ..

$window.alert('fire!');

May be the promise isn't ready when you call the alert, using .then() and .catch() could be the solution. See this too Promise on AngularJS resource save action


You might be having some error message at the console of the browser, an incorrect response from the server could throw an exception if it cannot be parse correctly your response from server, that btw should be the updated resource (json) or blank.

Check also this question that might be related


Need Your Help

bash/shell: easiest way to get kernel name components on command line

bash shell command-line kernel version

On my Fedora machine I sometimes need to find out certain components of the kernel name, e.g.

Open specific version of Git repo in explorer

git git-commit git-checkout

Is there a way to open a version of a Git repository within Windows explorer?