0

I need

batchdata:{course_name:"xyz",batch_fees:"1200",---,course_id:"xyz123"}

to sent database but I'm having problem to send course_id along with batchData for that I'm taking course_id from another service that is basically a course table So please look into the code I'm not accessing this -> response.data.data[0]._id because I cant access it outside the function since it local variable so is there any way so that I can bind course_id with batchData object...

  app.controller('batch_add', function($scope, $http) {
        $http.get("/courses/getall")
        .then(function(response) {
                if (response.data.length == 0) {
                    $scope.items = [{ course_name: "No data", course_desc: "No data", course_fees: "No data" }];
                } else {
                    $scope.items = response.data.data;
                    var course_name_items = []
                    for (var i = 0; i < $scope.items.length; i++) {
                      course_name_items.push($scope.items[i].course_name)
                    }
                    $scope.course_items = course_name_items;
                }
            });

        $scope.batch_status_items = ['Pending', 'Running', 'Finished', 'Canceled' ]



      $scope.batchData={};
      $scope.login =  {"batchData" : $scope.batchData};
      $scope.submitForm = function() {
      $scope.login =  {"batchData" : $scope.batchData};
      $scope.fail = false;
          $http({
              method  : 'POST',
              url: 'batches/add',
              data    : $scope.login
      })
          .success(function(data) {
              if (data.success == true) {
                  $scope.fail = true;
                  $scope.success = true;
                  $scope.success_message = data.message;
                  var courseName = $scope.batchData.course_name;
                  $http.get("/courses/search/"+courseName)
                  .then(function(response) {
                          if (response.data.length == 0) {
                            console.log("error");
                          } else {
                              $scope.batchData.course_id= response.data.data[0]._id

                          }
                      });
                          toastr.options = {"positionClass": "toast-bottom-right"}
                          Command: toastr["success"]("A new batch has been added!")
                  } else {
                      $scope.fail = true;
                      $scope.success = false;
                      $scope.error_message = data.message;
                      console.log($scope.success_message);
                     //  toastr.error('Something went wrong.', 'Ooops!')
                  }                      
            });
  $scope.batchData.course_id = response.data.data[0]._id
          };
      });
Rahil Ansari
  • 43
  • 10

1 Answers1

0

why don't you get course_id first, then send that id to database along with batchData later?

salix
  • 846
  • 6
  • 13
  • If you see course_name_items array it makes a select box in UI than we get a course name after selecting a course name and later than that courseName is a key to find a course_id so I can't get course _id first – Rahil Ansari Dec 14 '16 at 19:30
  • I meant finding the course_id before sending batchData, at the beginning of the submitForm function. – salix Dec 14 '16 at 19:34
  • glad to help :) – salix Dec 14 '16 at 20:51