admin管理员组文章数量:1335593
I'm trying to store all the user's profile photos in the realtime database, but I'm stucked with a problem.
When trying to upload the PhotoUrl with this code, this error shows up.
Code:
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 e 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.
});
});
Error:
Uncaught Error: Firebase.set failed: First argument contains undefined in property 'Usuarios.kD1tD1NAmsaGiW0zj6lFz9GDWro1.photoUri'.
When I do alert(user.photoUrl), it shows as 'undefined'.
The first thing I thought was that it can't be saved in database because it isn't a String, so I tried calling toString() from it, but I can't.
toString() error:
Uncaught TypeError: Cannot read property 'toString' of undefined
So, how can I save the photoUrl from a user (as String) in Firebase Database?
Thanks in advance.
I'm trying to store all the user's profile photos in the realtime database, but I'm stucked with a problem.
When trying to upload the PhotoUrl with this code, this error shows up.
Code:
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 e 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.
});
});
Error:
Uncaught Error: Firebase.set failed: First argument contains undefined in property 'Usuarios.kD1tD1NAmsaGiW0zj6lFz9GDWro1.photoUri'.
When I do alert(user.photoUrl), it shows as 'undefined'.
The first thing I thought was that it can't be saved in database because it isn't a String, so I tried calling toString() from it, but I can't.
toString() error:
Uncaught TypeError: Cannot read property 'toString' of undefined
So, how can I save the photoUrl from a user (as String) in Firebase Database?
Thanks in advance.
Share Improve this question asked Jul 29, 2017 at 6:23 thecalinthecalin 1061 gold badge1 silver badge6 bronze badges 1-
I know this is an old question, but I had the same issue today and I realized that I was trying to reference as
photoUrl
but the correct isphotoURL
, notice the capitalized URL – Juan Andrade Commented Jan 6, 2022 at 14:25
2 Answers
Reset to default 3For most I tried not to do this type of workarounds, ended up uploading the photo to Firebase Storage, then setting User.photoURL and "photoUri" DB field to the url of the uploaded image:
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.
});
});
});
});
The photoUrl is a nullable string as you can see in the documentation.
So you have to test if the value is set before use it.
For example :
if (typeof user.photoUrl != 'undefined') {
// Use user.photoUrl
}
本文标签: javascriptFirebase UserphotoUrl to StringStack Overflow
版权声明:本文标题:javascript - Firebase: User.photoUrl to String - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742392443a2466255.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论