firebase - react-native:firestore - 如何从存储桶引用中获取文件夹名称?
问题描述
如果我需要从文件夹中获取图像 url,那么我将执行以下操作:
const ref = storage().ref('foldername');
ref
.listAll()
.then(function (result) {
result.items.forEach(function (imageRef) {
// And finally display them
displayImage(imageRef);
});
})
.catch(function (error) {
console.log(error);
// Handle any errors
});
function displayImage(imageRef) {
imageRef
.getDownloadURL()
.then(function (url) {
// TODO: Display the image on the UI
console.log('urlurlurlurl', url);
})
.catch(function (error) {
// Handle any errors
});
}
但我需要先获取文件夹名称列表才能在应用程序中显示它们的名称。
例如 - 我的存储桶包含 3 个文件夹。每个文件夹包含 5 张图像。
我需要先获取这 3 个文件夹的列表。
我试图在这里找到答案,但找不到。
谢谢
更新: 这是我的带有 2 个目录的存储桶。这两个目录有图像文件
我需要先获取这些目录名称,以便在屏幕上显示它们。然后我将根据客户端的目录选择来获取文件。
解决方案
现在,您的代码仅通过迭代查看具有给定前缀的文件result.items
:
ref
.listAll()
.then(function (result) {
result.items.forEach(function (imageRef) {
// And finally display them
displayImage(imageRef);
});
})
如果您想知道嵌套前缀的名称(您称之为“文件夹”),您也必须进行迭代result.prefixes
。React Native 文档似乎没有提供这方面的示例,但您可以在Firebase 文档中看到它。
ref
.listAll()
.then(function (result) {
result.items.forEach(function (imageRef) {
// ...
});
result.prefixes.forEach(function (folderRef) {
// ...
});
})
推荐阅读
- python - 根据条件从列表中删除元素
- lit-html - render() - 函数没有更新 DOM - lit-html
- python - 类型错误:在执行 Seaborn Histplot 时,“int”和“str”实例之间不支持“<=”
- python - “dict”对象没有属性“y”。我对这意味着什么以及如何解决感到困惑
- powershell - 在 powershell 中的 stderr 上捕获文字字符串
- c++ - Visual Studio Code 不显示输出 (C++)
- xcode - 尝试查看 USDZ 文件时 Xcode 崩溃
- java - 禁用 Spring Boot Webclient 日志
- python - 如何在 PySpark 中导入 AnalysisException
- python-3.x - 如果找不到,如何返回“NA”而不是 null