【问题标题】:updating photoURL of a user by accessing from firebase storage通过从 Firebase 存储访问来更新用户的 photoURL
【发布时间】:2020-06-06 17:12:10
【问题描述】:

我正在使用此 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

我做错了什么

【问题讨论】:

    标签: javascript firebase-authentication firebase-storage


    【解决方案1】:

    您需要等待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) });    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      • 2019-01-14
      • 2018-07-12
      • 2020-01-14
      • 2018-05-30
      相关资源
      最近更新 更多