Jquery 创建Firebase存储的新唯一路径,并将其存储在数据库中?
我有一个MarketplaceWeb应用程序,用户可以上传项目,当然他们也可以看到与这些项目相关的图像。问题是如何组织存储桶,我正在考虑将路径设置为Jquery 创建Firebase存储的新唯一路径,并将其存储在数据库中?,jquery,html,firebase,firebase-realtime-database,firebase-storage,Jquery,Html,Firebase,Firebase Realtime Database,Firebase Storage,我有一个MarketplaceWeb应用程序,用户可以上传项目,当然他们也可以看到与这些项目相关的图像。问题是如何组织存储桶,我正在考虑将路径设置为itemmages/itemUID/image1.jpg。问题是获取itemUID,它是在将项目添加到数据库后自动生成的 下面是我用来向数据库添加项的代码片段: itemsRef.push({ title: title, description: description, tags: tags,
itemmages/itemUID/image1.jpg
。问题是获取itemUID
,它是在将项目添加到数据库后自动生成的
下面是我用来向数据库添加项的代码片段:
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
而不是假的链接,我没有任何线索(搜索也没有帮助):/
感谢您的帮助 为编写拙劣(未经测试)的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);
});
为写得不好(未经测试)的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);
});