Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

script.js

for(var i = 0;i<$scope.data_acc_lv.length;i++)
        {
            var input3 = {
                "_id": $scope.accLvID + i,
                "acc_id":$scope.accLvID,
                "name": $scope.data_acc_lv[i].names,
                "read": $scope.data_acc_lv[i].read,
                "execute": $scope.data_acc_lv[i].execute,
            }
            $http.post("http://localhost:1234/access_menu",input3)
            .success(function(res){
                if(res.error == 0)
                {

                }

            });
        } 

script.js

app.post('/access_menu',function(req,res){
    var IDMenu = req.body._id;
    var Forein = req.body.acc_id;
    var Name = req.body.name;
    var Read = req.body.read;
    var Execute = req.body.execute;

    var data = {"error":1,"Access_menu":""};
    if(!!Name && !!Read && !!Execute)
    {
        db.collection("access_menu").insert({_id:IDMenu,acc_id:Forein,name:Name,read:Read,execute:Execute},function(err,req){
            if(!!err)
            {
                data['Acess_menu'] = "input error";
            }
            else
            {
                data['error'] = 0;
                data['Access_menu'] = "input berhasil"
            }
            res.json(data);
        });
        data = {"error":1,"Access_menu":""};
    }
});

so I trying to input data from table to database but I always got the value from the last index (all previous value replace by the last index value), so what the cause the problem. Thank you

share|improve this question
    
Your example is unclear, can you provide a plunker pls? Or your Angular data bound code and html. .success is deprecated $http method. – Kindzoku Aug 19 at 9:30
    
like @Kindzoku said you should use .then().catch(). What is in $scope.accLvID ? – DMCISSOKHO Aug 19 at 9:33
    
Btw, does $scope.data_acc_lv contains array you require? – Kindzoku Aug 19 at 9:35
    
Do you have control over the Rest API? If so, this should be refactored to make a single request. – Martin Aug 19 at 10:06
up vote 0 down vote accepted

You can wrap the code inside the for loop in an immediately invoked function

     (function( i ) {
        var input3 = {
            "_id": $scope.accLvID + i,
            "acc_id":$scope.accLvID,
            "name": $scope.data_acc_lv[i].names,
            "read": $scope.data_acc_lv[i].read,
            "execute": $scope.data_acc_lv[i].execute,
        }
        $http.post("http://localhost:1234/access_menu",input3)
        .success(function(res){
            if(res.error == 0)
            {

            }

        });

      })(i);

Hope this helps.

share|improve this answer
    
He can do a lot of thing, but what for? :) – Kindzoku Aug 19 at 9:41
    
thank you for your help sir – Jan sebastian Aug 19 at 10:44
var temp = [];
for(var i = 0;i<$scope.data_acc_lv.length;i++){
       var input3 = {
          "_id": $scope.accLvID + i,
          "acc_id":$scope.accLvID,
          "name": $scope.data_acc_lv[i].names,
          "read": $scope.data_acc_lv[i].read,
          "execute": $scope.data_acc_lv[i].execute,
          }
         temp.push(input3)
} 
$http.post("http://localhost:1234/access_menu",temp )
        .success(function(res){
            if(res.error == 0)
            {

            }

        });

why not just format your array then send the whole array in on the post then loop in your script.js?

share|improve this answer
1  
Well, why not to use Array.map() than? $scope.data_acc_lv.map(function(item, index){ return { '_id: item.'... }; }); – Kindzoku Aug 19 at 9:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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