Cannot assign value to a variable - Javascript

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

Answers


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

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

Need Your Help

Why won't Hg let you push with patches applied?

mercurial mercurial-queue

I can't understand why Mercurial won't let me push when patches are applied. From my point of view, applied patches affect only the current workspace and pushing isn't influenced by the state of the

How to centre text in Sherlock Action Bar with icon and sliding menu

android android-actionbar actionbarsherlock slidingmenu

I am trying to implement an Action Bar on an Android Activity which implements Jeremy Feinstein's sliding menu and actionbarSherlock. The issue I am facing is to centre align the title text in the ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.