2016-08-11 2 views
0

Ich habe gerade meine Upload-Funktion vom Controller (wo es funktionierte, wie es sollte) in die Fabrik verschoben und es hörte auf zu arbeiten. Ich bin halte diese Fehler, aber ich weiß nicht,/verstehen, wo das ProblemHttp Anfrage Konfigurations-URL muss eine Zeichenfolge sein. Erhalten: undefined

angular.js:13550 Error: [$http:badreq] Http request configuration url must be a string. Received: undefined 
http://errors.angularjs.org/1.5.5/$http/badreq?p0=undefined 
    at angular.js:68 
    at $http (angular.js:11194) 
    at uploadWithAngular (ng-file-upload.js:91) 
    at sendHttp (ng-file-upload.js:144) 
    at upload (ng-file-upload.js:330) 
    at Scope.$scope.uploadDocument (locationsCtrl.js:131) 
    at fn (eval at compile (angular.js:14432), <anonymous>:4:338) 
    at expensiveCheckFn (angular.js:15485) 
    at callback (angular.js:25018) 
    at Scope.$eval (angular.js:17229) 

Dies ist meine Upload Dokument-Funktion in der Steuerung

$scope.uploadDocument = function(file) { 
    if($scope.documentName.length > 4) { 
     $scope.errorMsg = ''; 
     file.upload = Upload.upload(documentsFactory.uploadDocument(
      $scope.id_location, 
      $scope.documentName, 
      $scope.documentDescription, 
      file, 
      $scope.locationUniqueId 
     )); 
     file.upload.then(function (response) { 
      $scope.documentName = $scope.documentDescription = $scope.userFile = ''; 
      documentsFactory.getDocuments($scope.id_location).then(function (data) { 
       $scope.documents = data; 
      }); 
      $timeout(function() { 
       file.result = response.data; 
      }); 
     }, function (response) { 
      if (response.status > 0) 
       $scope.errorMsg = response.status + ': ' + response.data; 
     }, function (evt) { 
      // Math.min is to fix IE which reports 200% sometimes 
      file.progress = Math.min(100, parseInt(100.0 * evt.loaded/evt.total)); 
     }); 
    }else{ 
     $scope.errorMsg = 'Name should be at least 5 chars long'; 
    } 
}; 

Und dies ist mein Werk

factory.uploadDocument = function(id_location, name, description, file, locationUniqueId){ 
     return $http({ 
      method: 'POST', 
      url: $location.protocol() + '://' + $location.host() + '/rest/api/document/documents', 
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
      data: { 
       id_location: id_location, 
       name: name, 
       description: description, 
       userFile: file, 
       locationUniqueId: locationUniqueId 
      } 
     }).then(function successCallback(response){ 
      return response.data; 
     },function errorCallback(response){ 
      console.log('Error uploading documents: ' + response); 
     }); 
    }; 

UPDATE: Dies funktioniert Beispiel, wenn ich "Upload-Anforderung" in meinem Controller machen

file.upload = Upload.upload({ 
    url: $location.protocol() + '://' + $location.host() + '/rest/api/document/documents/', 
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
    data: { 
     id_location: $scope.id_location, 
     name: $scope.documentName, 
     description: $scope.documentDescription, 
     userFile: file, 
     locationUniqueId: $scope.locationUniqueId 
    } 
}); 

Wenn Sie irgendwelche zusätzlichen Entzündungen benötigen, lassen Sie es mich bitte wissen und ich werde zur Verfügung stellen. Vielen Dank im Voraus

+0

Wenn Sie sich "$ location.protocol() + ': //' + $ location.host() + '/ rest/api/Dokument/Dokumente'" vor dem $ http Anruf, was ist der Ausgang ? – tpsilva

+0

es ist in Ordnung (http://myapp.com/rest/api/document/documents/) –

Antwort

1

Nach dem Fehler-Stack:

Von ng-file-upload repository.

this.upload = function (config, internal) { 

, die dort von Ihnen genannt wird

Upload.upload(documentsFactory.uploadDocument(
     $scope.id_location, 
     $scope.documentName, 
     $scope.documentDescription, 
     file, 
     $scope.locationUniqueId 
    )); 

Linie 330

return sendHttp(config); 

li ne 144

uploadWithAngular(); 

Linie 91

$http(config).then(function (r) { 

Wo der Fehler ausgelöst wird. Es sieht so aus, als ob Upload.upload kein Versprechen akzeptiert, sondern eine Konfiguration für den $ http-Aufruf.


EDIT

Was die Config-Objekt zurückkehrt?

factory.uploadDocument = function(id_location, name, description, file, locationUniqueId){ 
    return { 
     method: 'POST', 
     url: $location.protocol() + '://' + $location.host() + '/rest/api/document/documents', 
     headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
     data: { 
      id_location: id_location, 
      name: name, 
      description: description, 
      userFile: file, 
      locationUniqueId: locationUniqueId 
     } 
    } 
}; 

Die beste Idee wäre, Upload in die Fabrik zu verschieben und das Versprechen zurückzugeben.

factory.uploadDocument = function(id_location, name, description, file, locationUniqueId){ 
    return Upload.upload({ 
     method: 'POST', 
     url: $location.protocol() + '://' + $location.host() + '/rest/api/document/documents', 
     headers: {'Content-Type': 'application/x-www-form-urlencoded'}, 
     data: { 
      id_location: id_location, 
      name: name, 
      description: description, 
      userFile: file, 
      locationUniqueId: locationUniqueId 
     } 
    }); 
+0

hm, thx dafür, aber können Sie bitte auch ein Beispiel machen, wie sollte ich Daten für den Aufruf von Upload.upload vorbereiten ({}) Funktion oder es ist unmöglich, dies über die Fabrik zu tun? –

+0

Ich habe den Arbeitscode hinzugefügt, wie es aussieht wie meine Controller-Funktion zum Hochladen, wenn ich eine Anfrage im Controller mache –

+0

Ich habe meine Antwort auch bearbeitet. Schau mal. Wenn Sie nur das Konfigurationsobjekt von Ihrem Dienst zurückgeben, kann das Problem möglicherweise behoben werden. – tpsilva

Verwandte Themen