0

I am having trouble assigning the value of "$scope.returnPollings[i]" to "plrtn" during the loop. "$scope.returnPollings" is an object with a data and i can confirm there is a data in it when i console.log it.

    db.transaction (function(tx) {
        var sql = "INSERT INTO STATUSREPLY (reply, name, userCode, icon, target, repliedDate) VALUES (?, ?, ?, ?, ?, ?)";
        var len = $scope.returnPollings.length;
        var plrtn;                              
        for (var i = 0; i < len; i++) {
            plrtn = $scope.returnPollings[i]; // value is not being assigned here
            tx.executeSql(sql, [plrtn.message, plrtn.name, plrtn.userCode, plrtn.qWink, plrtn.target, plrtn.createdDate ], 
                function(){ 
                        $scope.$apply(function(){
                            $scope.returnFstatus.push({
                                'message':$scope.ie.message, 
                                'createdDate':$scope.ie.createdDate, 
                                'icon':$scope.ie.qWink, 
                                'target':$scope.ie.target 
                                    });
                            })                                  
                    console.log('Status Inserted');
           },function(tx, error) {
                console.log('polling INSERT ERROR: ' + error.message);
                                 });
                            }

});

I will be glad if anyone can help me

David Addoteye
  • 1,587
  • 1
  • 19
  • 31

2 Answers2

1

The answer is, that you are assigning more than once to the same variable. i would do this like this:

plrtn;//without var
        for (var i = 0; i < len; i++) {
            delete plrtn;
            plrtn = $scope.returnPollings[i]; // value is not being assigned here
changtung
  • 1,614
  • 15
  • 19
  • Just be careful with this implementation. Without var, plrtn will be assigned as variable on the global scope, so that can affect some error in other part of the application. For more details please read the answer to this question: [What is the function of the var keyword and when to use it (or omit it)?](http://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-and-when-to-use-it-or-omit-it) – Tome Pejoski Jan 25 '15 at 21:31
0

Look at the value of $scope.returnPollings

var plrtn;  
console.log($scope.returnPollings);
for (var i = 0; i < len; i++) {

You get two values for $scope.returnPollings[0]
In the Console you should be able to see the two (length) values of
$scope.returnPollings[0] and $scope.returnPollings[1]

Also

$scope.returnPollings[0].message
$scope.returnPollings[0].name
$scope.returnPollings[0].userCode
$scope.returnPollings[0].q

$scope.returnPollings[1].message
$scope.returnPollings[1].name
$scope.returnPollings[1].userCode
$scope.returnPollings[1].q

In your SQL you do not need to use plrtn.

Instead you could use:

$scope.returnPollings[i].message
$scope.returnPollings[i].name
$scope.returnPollings[i].userCode
$scope.returnPollings[i].q
Misunderstood
  • 5,534
  • 1
  • 18
  • 25