我有添加项目到数据库的代码片段:
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存储创建新的唯一路径,并将其存储在数据库中?
发布评论