javascript - Firebase 没有使用 typescript 和 Ionic3 在元数据中返回 downloadURL
问题描述
几天来,我一直在努力让 Firebase 在元数据中返回 downloadURL。所有人都认为它应该在那里,因为元数据包括 fullPath 和其他信息,但不包括 downloadURL。
addItem(
itemName: string,
eventId: string,
itemPicture: string = null
): PromiseLike<any> {
return this.activityListRef
.child(`${eventId}/itemList`)
.push({ itemName })
.then(newItem => {
this.activityListRef.child(eventId).transaction(event => {
return event;
});
if (itemPicture != null) {
return firebase
.storage()
.ref(`/item/${newItem.key}/profilePicture.png`)
.putString(itemPicture, 'base64', { contentType: 'image/png' })
.then(savedPicture => {
console.log(savedPicture.metadata);
this.activityListRef
.child(`${eventId}/itemList/${newItem.key}/profilePicture`)
.set(savedPicture.downloadURL);
});
}
});
}
如果我在控制台中登录,我可以看到除 downloadURL 之外的所有内容
我也尝试更改 .set(savedPicture.downloadURL); 到 .set(savedPicture.metadata.downloadURLS[0]);
但在任何响应参数中仍然没有 downloadURL 项。
有任何想法吗?
解决方案
使用 putString 方法上传原始字符串后,使用相同的 ref 并使用 getDownloadUrl() 方法获取如下所示的 url,
storage()
.ref(`/item/${newItem.key}/profilePicture.png`).getDownloadUrl() // which returns promise in turn returns the image url once it's available.
在您的情况下,您可以执行以下操作
var storageRef = firebase.storage().ref(`/item/${newItem.key}/profilePicture.png`)
return storageRef.putString(itemPicture, 'base64', { contentType: 'image/png' })
.then(savedPicture => {
storageRef.getDownloadUrl()
.then(url =>{
this.activityListRef
.child(`${eventId}/itemList/${newItem.key}/profilePicture`)
.set(url);
});
});
希望这可以帮助
推荐阅读
- c++ - 在支持多种类型的宏中使用 printf
- c++ - Glade Gui 问题
- java - 如何检查 Enum 是否在 Java 中使用
- vue.js - 如何在 vue 中创建/更新羽毛笔印迹?
- java - 如何检查firebase数据库中2个随机键中的值?
- graphql - Gatsby 在获取 graphql 时在构建期间给出错误“错误:名称为“文件”的类型不存在”
- symfony - 如何使用 Symfony 4 从 Web URL 生成 pdf?
- rust - 不能使用 format_args!由于临时值在此语句的末尾被释放
- java - 为什么我在这里使用方法引用或 lambda 很重要?
- mysql - `utf8_general_ci' 是弃用字符集 UTF8MB3 的排序规则