AngularFire存储-TypeError:无法读取未定义的属性“ then”

时间:2019-11-07 09:52:03

标签: javascript angularjs firebase angularfire

我想在Firebase存储中上传文件,我想获得百分比上传,最后是下载URL。

我已经尽力了,但是我有:

  

TypeError:无法读取未定义的属性'then'

这是我的功能:

$scope.createCatalog = function(catalog,uid,setColor,setBg){
    console.log(catalog);
    StorageService.put(catalogs,uid,medias,catalog.logo).then(function(success){
      console.log(success);
    })
}

这是我在服务中的代码:

put : function(name,userID,item,data){
      var itemRef = firebase.storage().ref().child("users").child(userID).child(name).child(item);
      var itemFile = $firebaseStorage(itemRef);
      var uploadTask = itemFile.$put(data);
      uploadTask.$progress(function(snapshot) {
        var percentUploaded = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
        return percentUploaded
      });
      uploadTask.$complete(function(snapshot) {
        return snapshot.downloadURL
      });
}

谢谢大家!

1 个答案:

答案 0 :(得分:0)

尝试返回承诺:

put : function(name,userID,item,data){
      var deferred = $q.defer();
      var itemRef = firebase.storage().ref().child("users").child(userID).child(name).child(item);
      var itemFile = $firebaseStorage(itemRef);
      var uploadTask = itemFile.$put(data);
      uploadTask.$progress(function(snapshot) {
        var percentUploaded = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
        deferred.notify(percentUploaded);
      });
      uploadTask.$complete(function(snapshot) {
        deferred.resolve(snapshot.downloadURL);
      });
      return deferred.promise;
}

有关更多信息,请参见