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

编程入门 行业动态 更新时间:2024-10-07 13:20:59
本文介绍了为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存储创建新的唯一路径,并将其存储在数据库中?

本文发布于:2023-11-30 18:30:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651072.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   路径   并将其   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!