为Firebase存储创建新的唯一路径,并将其存储在数据库中? [英] Creating a new unique path to Firebase Storage, coupled with storing it in the database?

查看:114
本文介绍了为Firebase存储创建新的唯一路径,并将其存储在数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我有添加项目到数据库的代码片段:

  itemsRef.push({
title:title,
描述:描述,
标签:tags,
价格:price,
});

以下是我用来存储图片的简化函数:

  var uploadTask = imageNewItemRef.child('fakeUID'+'/'+ imageNames [x])。putString(images [x],'base64'); 
$ b uploadTask.on('state_changed',function(snapshot){

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

正如你所看到的,我使用了一个硬编码的 fakeUID 链接为测试目的,但我没有线索(和搜索没有帮助),如何有一个 uniqueUID 而不是假的,这是链接到该项目:/ $ / $>

任何帮助表示赞赏!

解决方案

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



  //创建一个新的推送ID和用
var currentItemRef = itemsRef.push({
title:title,
description:description,
tags:tags,
price:price,
})。then(function(){
var storageRef = firebase.storage()。ref();
// currentItemRef.name是DB
中的唯一键返回storageRef.child(currentItemRef.name +'/'+ imageNames [x])。putString(images [x],'base64');
})。then(function(snapshot){
/ /用do来更新数据库wnload URL
return currentItemRef.update({
url:snapshot.metadata.downloadURLs [0]
}); catch(function(error){
console.error(error);
});


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, I was thinking to make the path itemImages/itemUID/image1.jpg. The problem is getting the itemUID, which is automatically generated, after an item is added to the database.

Here's a code snippet I have for adding items to the db:

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

and here's a simplified function I use to store an image:

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);
});

as you can see, I'm using a hardcoded fakeUID link for testing purposes, but I have no clue (and searching hasn't helped), on how to have a uniqueUID instead of a fake one, that is linked to the item :/

Any help is appreciated!

解决方案

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);
});

这篇关于为Firebase存储创建新的唯一路径,并将其存储在数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆