if - Else if condition being ignored by browser

Asked

Viewed 64 times

1

Guys I have the following if - Else if condition for file upload check, depending on the file it allocates at a position of the array, however when arriving the name to do the validation it ignores the ifs and if Else entering in all the loops overwriting everything where it should not.

Using Angularjs, I’ve debugged and even tried to change the if to switch, but it keeps ignoring and entering all the loops overwriting everything. Does anyone know the reason, cause or circumstance of this witchcraft? I know that the method is not the best and I will even modify but I wanted to understand why it enters in all.

If the recipe is "recipe-pie" it should enter only the second loop and ignore the first and third, but it goes into all by overwriting their information.

$scope.setContentToDocument = function(value, name) {

    if ($scope.data.documents != undefined) {
        if ($scope.data.documents.length <= 3) {
            let object = {
                documentNumberOrName: name,
                fieldNumOrName: false,
                fieldNumOrNameDI: false,
                fieldNumOrNameEstimativa: false,
                fileUploadRequired: false,
                requiredDoc: false,
                type: "RECEITAS",
            }

            let file = {};

            if (name == "receita-bolo") {
                file = {
                    content: value.contentAsString,
                    name: value.fileName,
                };

                if ($scope.data.documents[0] != undefined) {
                    $scope.data.documents[0].file.name = value.fileName;
                    $scope.data.documents[0].file.content = value.contentAsString;
                } else {
                    object.file = file;
                    $scope.data.documents.push(object);
                }

            } else if (name == "receita-torta") {
                file = {
                    content: value.contentAsString,
                    name: value.fileName,
                };

                if ($scope.data.documents[1] != undefined) {
                    $scope.data.documents[1].file.name = value.fileName;
                    $scope.data.documents[1].file.content = value.contentAsString;
                } else {
                    object.file = file;
                    $scope.data.documents.push(object);
                }
            } else if (name == "receita-pizza") {
                file = {
                    content: value.contentAsString,
                    name: value.fileName,
                };

                if ($scope.data.documents[2] != undefined) {
                    $scope.data.documents[2].file.name = value.fileName;
                    $scope.data.documents[2].file.content = value.contentAsString;
                } else {
                    object.file = file;
                    $scope.data.documents.push(object);
                }
            }
            console.log($scope.data.documents)
        }
    }
};
  • 1

    " entering into all the bonds " what ties? I don’t see any in this code. You only create the variable "Object" if that condition is true if ($scope.data.documents.length <= 3), and if she doesn’t go and enter the ifs below will give error of Undefined in the variable "Object". ifs are correct, you do not see how you can enter all of them. Also, the variable "file" is always the same, could declare only once instead of let file = {};

  • It’s true, there are no ties to the sleep I was ended up using wrong term hahaha. So, although I also don’t see how to enter all, the variables within each if end up being changed and the entire $Scope.data.Documents[] array

1 answer

0


The if / else if certainly will not enter at all, because the conditions are different, and the variable name is not changed in the process, but your code can be improved to become simpler, of course, and smaller. Below is a suggestion:

$scope.setContentToDocument = function(value, name) {

    if ($scope.data.documents != undefined) {
           
      let object = {
        documentNumberOrName: name,
        fieldNumOrName: false,
        fieldNumOrNameDI: false,
        fieldNumOrNameEstimativa: false,
        fileUploadRequired: false,
        requiredDoc: false,
        type: "RECEITAS",
        file: {
          content: value.contentAsString,
          name: value.fileName,
        }
      };

      if (name == "receita-bolo") {
        $scope.data.documents[0] = object;
      } else if (name == "receita-torta") {
        $scope.data.documents[1] = object;
      } else if (name == "receita-pizza") {
        $scope.data.documents[2] = object;
      }

      console.log($scope.data.documents)
    }
};

Making $scope.data.documents.push(object) is adding in the "next" free element, and does not guarantee that for example if "recipe-cake" comes, it will be added to element "0".

Already doing so $scope.data.documents[0] = object guarantees the correct index, assuming that it will always set the 3, otherwise it needs one more logic before setting the element

  • Boy, you saved my life. The way it was before, it somehow replaced all the existing attachments when uploading the Pizza Recipe. The $Scope.data.Documents.push(Object) that should be messing everything up, because in addition to adding an empty Dice every time the upload button was triggered, it inserted it in the wrong place.

  • good, wait for right now. If the answer helped you or solved your doubt, do not forget to vote or accept the answer :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.