0

I have this function

$scope.updateValue = function(key, selectedProductname, numberUsed){

var selectedKey = key;
var selectedProductname = selectedProductname;
var numberUsed = numberUsed;

var useageRef = ref.child('/useage/');
var updateObj = $firebase(useageRef);

var myData = {
    productName : selectedProductname,
    numberUsed : numberUsed
}

var decrementLocation = inventoryRef.child(key + '/amount')

updateObj.$push(myData).then(

    decrementLocation.transaction(function (current_value, numberUsed) {
           console.log(numberUsed);
          return (current_value || 0) - 1;

})

    );  
}

I pass "numberUsed" into $scope.updateValue and use it inside myData and then push it to the server and there is no problem with that but when I use it at this line "decrementLocation.transaction(function (current_value, numberUsed) {" and then I try to console.log(numberUsed); the console says undefined. Why? and how can I use numberUsed in this line "decrementLocation.transaction(function (current_value, numberUsed) {" ? how to code it successfully?

1 Answer 1

1

There is a number of things going on here.

First of all, in the following code:

    decrementLocation.transaction(function (current_value, numberUsed) {
       console.log(numberUsed);
      return (current_value || 0) - 1;
    })

You are re-declaring numberUsed as the second parameter of the .transaction() callback function. Thus, whatever numberUsed was outside of this small function does not matter. If you want to use the var from the surrounding function, you would need to do:

    decrementLocation.transaction(function (current_value) {
       console.log(numberUsed);
      return (current_value || 0) - 1;
    })

Second of all, there is no closing ; to your .transaction() function. I don't think it will materially affect your operating here, but cannot be sure. This should be run through jslint/jshint.

Third, you are redeclaring numberUsed inside the entire surrounding $scope.updateValue() function.

$scope.updateValue = function(key, selectedProductname, numberUsed){
    var numberUsed = numberUsed;

So you are declaring a new variable numberUsed, whose value will be, well, numberUsed, but it is a new var, so it should be set to undefined. If it is set to anything at all, that would be surprising. If you need the var, then you should do:

$scope.updateValue = function(key, selectedProductname, numberUsed){
    var nu2 = numberUsed;

or something similar. But even then, why bother to redeclare the var? It is copied by value anyways.

A good linter will catch any of this.

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

2 Comments

alright, thank you so much. I have edit everything to be what you have said and it works fine. So, in conclusion when these get passed into: $scope.updateValue = function(key, selectedProductname, numberUsed) the key, selectedProductname and numberUsed are already a variable that I can use anywhere in the function and the function that nested inside it without redeclaring it? Am I understand correctly? ^^
If you pass them in when you call the function $scope.updateValue() then yes.

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.