javascript - Angular JS and Ionic - Error on $http.post, help me understand -


i trying upload file ios, have unexpected error. html:

      <div ng-repeat="photo in vendordata.galerias" class="elemento">         <div style="z-index: 10;">           <img class="thumb" ng-src="{{photo.url}}" />         </div>         <div class="" style="text-align: center;">           <button class="" href="#" ng-show="photo.borrar">eliminar</button>           <button class="" href="#" ng-click="subirfoto($index+1)" ng-hide="photo.borrar">subir foto</button>         </div>       </div> 

the controller methods:

$scope.subirfoto = function(identificador){     $scope.elementosek = identificador;     console.log("img src seleccionado:"+$scope.elementosek);     $scope.getphoto(picturesource.photolibrary);   };   $scope.getphoto = function (source) {       // retrieve image file location specified source       navigator.camera.getpicture(onphotourisuccess, onfail, { quality: 50,         destinationtype: destinationtype.file_uri,         sourcetype: source });   };   function onphotourisuccess(imageuri) {     // uncomment view image file uri      // image handle     var previo = $scope.vendordata.galerias[$scope.elementosek].url;        var file = imageuri;     var uploadurl = closer_server.url+'/upload_vendor';     var uploadresult = fileupload.uploadfiletourl(file, uploadurl, $scope.elementosek, session.getid(), 'vendor');      console.log(uploadresult);     if(uploadresult.response == "200"){       $scope.vendordata.galerias[$scope.elementosek].url = imageuri;       $scope.$apply();     }   } 

and service fileupload:

app.service('fileupload', ['$http', function ($http) {     this.uploadfiletourl = function(file, uploadurl, numero, sessionide, typepic){         var fd = new formdata();          fd.append('file', file);          var data = {           session : sessionide,           num_foto: numero,           type : typepic          };          fd.append("data", json.stringify(data));          $http.post(uploadurl, fd, {             transformrequest: angular.identity,             headers: {'content-type': undefined}         })         .then(function(data){           console.log("response ok: "+json.stringify(data));           return json.stringify(data);         }, function(){           console.log("response fail");           return "error subiendo archivo";         });     } }]); 

what does? in image gallery of 6 elements, click "upload" (subir) button, ios image gallery loaded picking image. when pick image, image uri sent fileupload service. method uploadfiletourl() send server $http.post. in moment throws odd error cannot understand:

typeerror: undefined not object (evaluating 'uploadresult.response')

callbackfromnativecordova.js:306

(función anónima)cordova.js:1102

nativeevalandfetchcordova.js:1110

nativecallbackcordova.js:1099

(función anónima)index.html:1

using console.log had checked error happens in service fileupload between line fd.append("data", json.stringify(data)); , line console.log("response ok: "+json.stringify(data));

what error???

are sure error occurs before http post?

seems in case of failure on fileupload service, return string, when try access result, treat json object:

.then(function(data){       console.log("response ok: "+json.stringify(data));       return json.stringify(data);     }, function(){       console.log("response fail");       return "error subiendo archivo";     }); 

you need validation here:

if(angular.isobject(uploadresult) && uploadresult.response == "200"){   $scope.vendordata.galerias[$scope.elementosek].url = imageuri;   $scope.$apply(); } 

you make use of ng-file-upload, if can't/don't want use directives, still can use de upload service.

good luck!


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -