【问题标题】:Creating a new unique path to Firebase Storage, coupled with storing it in the database?为 Firebase 存储创建新的唯一路径,并将其存储在数据库中?
【发布时间】:2016-11-21 05:52:54
【问题描述】:

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

这是我用于向数据库添加项目的代码 sn-p:

    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 而不是假链接(搜索也没有帮助),链接到该项目:/

感谢任何帮助!

【问题讨论】:

    标签: jquery html firebase firebase-realtime-database firebase-storage


    【解决方案1】:

    对于编写不佳(且未经测试)的 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);
    });
    

    【讨论】:

      猜你喜欢
      • 2016-10-22
      • 1970-01-01
      • 2020-08-23
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 2020-12-09
      相关资源
      最近更新 更多