Firebase:User.photoUrl 到字符串

the*_*lin 6 javascript web firebase

我试图将所有用户的个人资料照片存储在实时数据库中,但遇到了问题。

当尝试使用此代码上传 PhotoUrl 时,会出现此错误。

代码:

var user = firebase.auth().currentUser;

user.updateProfile({

  // Here, "name" and "imgUrl" are parameters of parent function.
  // I don't think this is the reason, but if it helps,
  // "imgUrl" always come from a <img> src.
  displayName: name,
  photoUrl: imgUrl

}).then(function(){

  // Here is where I try to upload all to the database
  firebase.database().ref("/Usuarios/" + user.uid).set({
    "email": email,
    "name": name,
    "photoUri": user.photoUrl, // This is where I think the problem originates.
    "uid": user.uid  // This calls user again, and it works perfectly.
  });

});
Run Code Online (Sandbox Code Playgroud)

错误:

Uncaught Error: Firebase.set failed: First argument contains undefined in property 'Usuarios.kD1tD1NAmsaGiW0zj6lFz9GDWro1.photoUri'.
Run Code Online (Sandbox Code Playgroud)

当我执行alert(user.photoUrl)时,它显示为“未定义”。

我首先想到的是它不能保存在数据库中,因为它不是字符串,所以我尝试从中调用 toString(),但我不能。

toString() 错误:

Uncaught TypeError: Cannot read property 'toString' of undefined
Run Code Online (Sandbox Code Playgroud)

那么,如何将用户的 photoUrl(作为字符串)保存在 Firebase 数据库中?

提前致谢。

the*_*lin 3

对于大多数人来说,我尝试不执行这种类型的解决方法,最终将照片上传到 Firebase Storage,然后将User.photoURL“photoUri” DB 字段设置为上传图像的 url

user = firebase.auth().currentUser;
avatarStgRef = firebase.storage().ref("Usuarios/" + user.uid + "/avatar.jpg");

// imgFile the Photo File.
avatarStgRef.put(imgFile).then(function(snapshot){
    snapshot.ref.getDownloadURL().then(function(url){  // Now I can use url
        user.updateProfile({
            photoURL: url       // <- URL from uploaded photo.
        }).then(function(){
            firebase.database().ref("Usuarios/" + user.uid).set({
              "photoUri": url   // <- URL from uploaded photo.
            });
        });
    });
});
Run Code Online (Sandbox Code Playgroud)