javascript - 通过从 Firebase 存储访问来更新用户的 photoURL
问题描述
我正在使用此 javascript 代码将图像上传到 firebase 存储,然后我想将此图像 url 更新为用户的 photoURL (firebase auth)
const fileButton = document.querySelector('#fileButton');
var imageURL;
fileButton.addEventListener('change', function(e) {
//get file
var file = e.target.files[0];
//create a storage reference
var storageRef = storage.ref('profilePictures/' + userUID + '.jpg');
//upload file
var task = storageRef.put(file);
//update progress bar
task.on('state_changed',
function progress(snapshot){
},
function error(err) {
},
function complete(){
storageRef.getDownloadURL().then(function(url) {
console.log(url);
imageURL = url;
})
.catch(function(error) {
// Handle any errors
console.log(error);
});
// window.location.replace('/profile');
var user = auth.currentUser;
user.updateProfile({
photoURL: imageURL
})
.then(function() {
// Update successful.
console.log(user);
})
.catch(function(error) {
// An error happened.
console.log(error);
});
}
);
});
存储上的图像上传成功 ,但用户数据中的 photoURL不成功
uid: "t2TbJS6ut7NZH4UU8HeAVarGPOw2"
displayName: "Sachin Kumar"
photoURL: null
email: "s@gmail.com"
emailVerified: false
我做错了什么
解决方案
您需要等待storageRef.getDownloadURL()
完成才能将其传递给user.updateProfile
喜欢:
function complete() {
storageRef.getDownloadURL().then(function(url) {
console.log(url);
imageURL = url;
// Now you have valid `imageURL` from async call
var user = auth.currentUser;
user.updateProfile({ photoURL: imageURL })
.then(function() { console.log(user) })
.catch(function(error) { console.log(error) });
})
.catch(function(error) { console.log(error) });
}
推荐阅读
- node.js - 如何从在 AWS ecs 上使用 docker 部署的应用程序获取日志
- sql - 如果为空则插入数据
- python - 使用 python3 将值插入 SQLite3 DB 表时出错
- linux - 我在 Linux 中的 Windows 文件夹在哪里?
- android - 使用 API 注册后,真实 android 设备中的网络请求失败错误
- java - 在 IntelliJ 2018.1 的编辑器中显示所选类名的源代码
- python - 如何在python中的while循环结束时打印所有输出
- c# - Winform无法添加elementHost
- middleware - MassTransit 中间件:不带 Automatonymous 的 Saga 的 OnMissingInstance 等效项
- ios - DJI sdk 获取 RTK 信息