代码之家  ›  专栏  ›  技术社区  ›  thecalin

Firebase:用户。字符串的photoUrl

  •  5
  • thecalin  · 技术社区  · 9 年前

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

    当尝试上载带有此代码的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.
      });
    
    });
    

    Uncaught Error: Firebase.set failed: First argument contains undefined in property 'Usuarios.kD1tD1NAmsaGiW0zj6lFz9GDWro1.photoUri'.
    

    当我这么做的时候 警报(user.photoUrl)

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

    toString()错误:

    Uncaught TypeError: Cannot read property 'toString' of undefined
    

    提前谢谢。

    2 回复  |  直到 9 年前
        1
  •  3
  •   thecalin    9 年前

    对于大多数人来说,我试图不做这种变通方法,结果 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.
                });
            });
        });
    });
    
        2
  •  0
  •   Pipiks    9 年前

    photoUrl 是一个可为null的字符串,如文档中所示。

    因此,在使用之前,您必须测试该值是否已设置。

    if (typeof user.photoUrl != 'undefined') {
        // Use user.photoUrl
    }