用于获取url图片并存储到firebase数据库中的代码

时间:2018-11-23 02:00:02

标签: javascript firebase firebase-realtime-database firebase-storage

html

未捕获(承诺)错误错误:Reference.update失败:第一个参数在属性“ Posts-LRyHOS3r8-VP-7WMsCS.url”中包含未定义,并且不存储在firebase数据库中。这是我的代码

1 个答案:

答案 0 :(得分:0)

该错误消息非常明显:downloadURL似乎是null。如果您搜索有关此问题的最新问题,则会看到现在通过调用getDownloadURL()异步检索下载URL。

类似这样:

uploadTask.on('state_changed', function(snapshot){
  // Observe state change events such as progress, pause, and resume
  // See below for more detail
}, function(error) {
  // Handle unsuccessful uploads
}, function() {
  var postKey = firebase.database().ref('Posts').push().key;
  var downloadURL = uploadTask.snapshot.downloadURL;
  return storageRef.getDownloadURL().toPromise().then(downloadUrl => {
    var postData = {
      url: downloadUrl,
      caption: $("#imageCaption").val()
    };

    var updatess = {};
    updatess ['Posts' + postKey]  = postData;

    return firebase.database().ref().update(updatess);
  });     

});

另请参阅: