Creating a new unique path to Firebase Storage, coupled with storing it in the database?

前端 未结 1 1429
生来不讨喜
生来不讨喜 2020-12-22 08:09

I have a marketplace web app, where users can upload items, and of course they can also see images associated with these items. The problem is organizing the storage buckets

相关标签:
1条回答
  • 2020-12-22 08:48

    Apologies for poorly written (and untested) JS, but would something like this work?

    // 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);
    });
    
    0 讨论(0)
提交回复
热议问题