首页 文章

创建Firebase存储的新路径,并将其存储在数据库中?

提问于
浏览
0

我有一个市场网络应用程序,用户可以上传项目,当然他们也可以看到与这些项目相关的图像 . 问题是组织存储桶,我正在考虑制作路径 itemImages/itemUID/image1.jpg . 问题是在将项目添加到数据库后获取 itemUID ,这是自动生成的 .

这是我用于向db添加项目的代码片段:

itemsRef.push({
        title: title,
        description: description,
        tags: tags,
        price: price,
    });

这是我用来存储图像的简化函数:

var uploadTask = imageNewItemRef.child('fakeUID' + '/' +  imageNames[x]).putString(images[x], 'base64');

uploadTask.on('state_changed', function(snapshot) {

}, function(error) {
    console.log("error uploading image");
}, function() {
    var downloadURL = uploadTask.snapshot.downloadURL;
    console.log(downloadURL);
});

正如你所看到的,我正在使用硬编码的 fakeUID 链接进行测试,但我没有任何线索(并且搜索没有帮助),关于如何使用 uniqueUID 而不是假的链接到项目: /

任何帮助表示赞赏!

1 回答

  • 2

    为写得不好(和未经测试)的JS道歉,但是这样的工作会不会这样?

    // create a new push ID and update the DB with some information
    var currentItemRef = itemsRef.push({
        title: title,
        description: description,
        tags: tags,
        price: price,
    }).then(function() {
      var storageRef = firebase.storage().ref();
      // currentItemRef.name is the unique key from the DB
      return storageRef.child(currentItemRef.name + '/' + imageNames[x]).putString(images[x], 'base64');
    }).then(function(snapshot) {
      // update the DB with the download URL
      return currentItemRef.update({
        url: snapshot.metadata.downloadURLs[0]
      });
    }).catch(function(error) {
      console.error(error);
    });
    

相关问题