0

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.

4 Answers 4

1

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.

0

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!');
3
  • Hmm .. What if you did a console.log instead?
    – dcodesmith
    Jan 31, 2014 at 12:47
  • No luck with console either. Very odd.
    – Joe
    Jan 31, 2014 at 12:50
  • Might be a promise and not async.
    – dcodesmith
    Jan 31, 2014 at 12:57
0

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

0

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

2
  • If there was an incorrect response from server, then the database would not be updated. But it is getting updated and it's also reflected in my view.
    – Joe
    Jan 31, 2014 at 13:39
  • The serve could be saving correctly but that doesn't mean that you are sending the "expected" data that is this case is a json with of the updated resource (product). Did you check the console at browser? Any message?
    – dimirc
    Jan 31, 2014 at 13:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.