cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X

Problem with Javascript Object : TypeError

leonardosella
12-Amethyst

Problem with Javascript Object : TypeError

Hi everyone, 

In my project I load a json object from thingworx and want to manipulate the content of this file.json. 

 

The upload of the file from TW to Studio works fine, the code that is used for is: 

// -- APERTURA FILE JSON --
var MODEL_JSON = {
  'carrello': 'prova.json'
}
console.log('load MODEL_JSON files');

$scope.GetJsonFromTwxRepository = function (path) {
  $scope.$applyAsync(function () {
    $rootScope.$broadcast('app.mdl.Thing-Repository.svc.LoadJSON',
      {
        "path": path
      }
    );
  }
    , 500);
  console.log("after call of GetJsonFromTwxRepository")
};

$scope.modelDataDynamic = [];
$scope.$on('$ionicView.afterEnter', function () {
  console.log('Calling service');
  angular.forEach(MODEL_JSON, function (value, key) { // nel nostro caso: value --> 'carrello', key --> 'prova.json'
    $scope.GetJsonFromTwxRepository('/' + value);
    console.log(value);
    console.log("$ionicView.afterEnter was called ");
    $scope.$root.$on('LoadJSON-complete', function (event, args) {
      console.log("LoadJSON-complete event");
      //will print to console the event name - LoadJSON-Complete
      //console.log("name=" + event.name)
      //console.log(event);
      //console.log(args.data);
      // the data was the event returns
     
      $scope.COMP_LOCs = args.data // here we will set the data to global vars
     
      //console.log(JSON.stringify($scope.COMP_LOCs))//print to the console to check the data
      $scope.modelDataDynamic[key] = args.data;
      //this a double check we will convert it to string and parse it back to json
      //this will ensure that data is correct and has the correct syntax
    }
    );
  
  });
}
)

After launching the preview mode of Vuforia, I could see the object like represented in the attached image.  

The Json file has the following structure:

{"repair_sequence": [{
    "traino":[ { "type": "sequence_isometric_1", "lang":"it", "description":[  "Rimuovere la flangia indicata in rosa",
                                                    "Rimuovere lamiera indicata in rosa svitando le apposite viti di serraggio",
                                                    "Svitare le viti e rimuovere lamiera di copertura posteriore",
                                                    "Togliere la lamiera superiore svitando viti di serraggio", 
                                                    "Rimuovere il listello per procedere ad ispezione della trasmissione meccanica" 
                        ] },
                    { "lang":"en", "description":[  "Remove the flange indicated in pink", 
                                                    "Remove the sheet indicated in pink by unscrewing the appropriate tightening screws",
                                                    "Unscrew the screws and remove the rear cover plate",
                                                    "Remove the upper sheet by unscrewing the clamping screws",
                                                    "Remove the strip to inspect the mechanical transmission" 
                                                    ]}
    ]},
    {
    "traino":[ { "type": "sequence_isometric_2", "lang":"it", "description":[  "Rimuovere la trasmissione meccanica dal carrello",
                                                        "Rimuovere connettore Rockwell indicato dal flash rosa",
                                                        "Rimuovere il motore indicato in rosa, fare attenzione all'estrazione dell'albero. ",
                                                        "Rimuovere il basamento estraendo le viti di supporto" 
                                                        ] },
                        { "lang":"en", "description":[  "Remove the mechanical transmission from the carriage", 
                                                        "Remove Rockwell connector indicated by the pink flash",
                                                        "Remove the motor indicated in pink, pay attention to the extraction of the shaft",
                                                        "Rimuovere il basamento estraendo le viti di supporto" 
                                                       ] }
    ]                                  
}

]
}

 When I try to access to one string typed into the file in inspection mode, the console text: "TypeError: Cannot read property 'carrello' of undefined". 

The print command that I used for this: console.log($scope.modelDataDynamic.repair_sequence[0].traino[0].description[0]);

 

Anyone has a solution? Thank you

5 REPLIES 5

Hello @leonardosella ,

in one of my projects I did some similar  - I am not sure but I believe to remember that have same issue. Therefore I used some parse and stringify... to check the content with console.warn or the error in the console

 $scope.$root.$on('LoadJSON-complete', function(event, args) { 
  console.log("LoadJSON-complete event");
    $scope.speak("GetPropertyValues-complete event")
   console.log("name="+event.name)
   console.warn(args.data);
   $scope.COMP_LOCs=args.data
   console.log(JSON.stringify( $scope.COMP_LOCs))
    let json_new_obj=JSON.parse(JSON.stringify( $scope.COMP_LOCs))
      console.warn(json.new_obj)
});

 

 In this case you can check in the debugger console ( Shift- Strg -I) what is the structure of the received object to deside later how to access this object.

In your code :

angular.forEach(MODEL_JSON, function (value, key) {}

 
will call the function for the MODEL_JSON one time where
key='carrello'
and value ='prova.json'

In this case you have  some thing like:

$scope.modelDataDynamic['carrello'] = args.data



So means I belive here the syntax should be something like

console.log($scope.modelDataDynamic['carrello']['repair_sequence'][0].traino[0].description[0]);


to get the string "Rimuovere la flangia indicata in rosa",

 

 

I tried this: 

console.log($scope.modelDataDynamic['carrello']['repair_sequence'][0].traino[0].description[0]);

In the debugger console the answer was: 

 

TypeError: Cannot read property 'repair_sequence' of undefined  

 

I'm a bit confused. Any help?

what is the print of:

 

 

console.warn($scope.modelDataDynamic);
console.log(JSON.stringify($scope.modelDataDynamic));

 

 

I think in this case it seem to be undefined 

In your code you can decomment:

 

 $scope.COMP_LOCs = args.data // here we will set the data to global vars
console.log("Json received event::");    
console.log(JSON.stringify($scope.COMP_LOCs))//print to the console to check the data
      $scope.modelDataDynamic[key] = args.data;

 

to see if the service received the object and what is the structure of the received object

Thank you so much for your support, the print is attached in screen_esito_json. 

when we continue by adding next print

 

$scope.COMP_LOCs = args.data // here we will set the data to global vars
console.log("Json received event::");    
console.log(JSON.stringify($scope.COMP_LOCs))//print to the console to check the data
      $scope.modelDataDynamic[key] = args.data;
console.log(JSON.stringify($scope.modelDataDynamic['carrello']['repair_sequence']));

 

after the assignment - may be later you will reset the variable in your code- modelDataDynamic.

 

Top Tags