无法为变量赋值 - Javascript

时间:2015-01-25 19:12:12

标签: javascript angularjs sqlite

我无法将“$ scope.returnPollings [i]”的值分配给“plrtn” 在循环中。 “$ scope.returnPollings”是一个带有数据的对象,当我在console.log中时,我可以确认其中有数据。

    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);
                                 });
                            }

});

如果有人能帮助我,我会很高兴

2 个答案:

答案 0 :(得分:1)

答案是,您要为同一个变量分配多次。 我会这样做:

plrtn;//without var
        for (var i = 0; i < len; i++) {
            delete plrtn;
            plrtn = $scope.returnPollings[i]; // value is not being assigned here

答案 1 :(得分:0)

查看$scope.returnPollings

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

您获得$scope.returnPollings[0]的两个值  在控制台中,您应该能够看到两个(长度)值为$scope.returnPollings[0]$scope.returnPollings[1]

还有

$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

在SQL中,您不需要使用plrtn

相反,您可以使用:

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