admin管理员组文章数量:1399013
I'm trying to retrieve an image url from Firebase Storage and then setting an image with that url. However, it seems that I am setting the src to an undefined value with my current code:
This is my function I'm using to retrieve from Firebase Storage
import {Firebase,
FirebaseAuth,
FirebaseDatabase,
FirebaseStorage} from '../Initialize'
export function getProfilePictureUrl(uid, callback, onErrorCallback) {
var pathReference = FirebaseStorage.ref('profiles/' + uid + '/profilePicture.jpeg');
pathReference.getDownloadURL().then((url) => {
callback(url);
}).catch((error) => {
onErrorCallback(error);
});
}
I call it from a React Component that uses the function like this:
render() {
let profilePictureUrl = getProfilePictureUrl(uid, (url) => {
console.log(url); // The console.log(url) returns a valid and working url for the image. So I know my imports are correct
return url;
},
(error) => {
console.log(error.code);
return "";
})
return (
<img
src={profilePictureUrl}
/>
);
}
The image is not loaded properly as ProfilePictureUrl returns undefined.
I also used tried to make a tester inside render() like this:
render() {
if(profilePictureUrl !== undefined) {
console.log("defined");
}
else {
console.log("undefined");
}
// returns 'undefined'
}
And I was being logged the else response indicating that the function was returning a undefined value. My suspicion is that it has something to do with Firebase's asynchronous nature, but I am not sure how to solve it.
This question may be related to: React-Native: Download Image from Firebase Storage
I'm trying to retrieve an image url from Firebase Storage and then setting an image with that url. However, it seems that I am setting the src to an undefined value with my current code:
This is my function I'm using to retrieve from Firebase Storage
import {Firebase,
FirebaseAuth,
FirebaseDatabase,
FirebaseStorage} from '../Initialize'
export function getProfilePictureUrl(uid, callback, onErrorCallback) {
var pathReference = FirebaseStorage.ref('profiles/' + uid + '/profilePicture.jpeg');
pathReference.getDownloadURL().then((url) => {
callback(url);
}).catch((error) => {
onErrorCallback(error);
});
}
I call it from a React Component that uses the function like this:
render() {
let profilePictureUrl = getProfilePictureUrl(uid, (url) => {
console.log(url); // The console.log(url) returns a valid and working url for the image. So I know my imports are correct
return url;
},
(error) => {
console.log(error.code);
return "";
})
return (
<img
src={profilePictureUrl}
/>
);
}
The image is not loaded properly as ProfilePictureUrl returns undefined.
I also used tried to make a tester inside render() like this:
render() {
if(profilePictureUrl !== undefined) {
console.log("defined");
}
else {
console.log("undefined");
}
// returns 'undefined'
}
And I was being logged the else response indicating that the function was returning a undefined value. My suspicion is that it has something to do with Firebase's asynchronous nature, but I am not sure how to solve it.
This question may be related to: React-Native: Download Image from Firebase Storage
Share Improve this question edited May 23, 2017 at 12:34 CommunityBot 11 silver badge asked Jan 2, 2017 at 4:47 Michael LaiMichael Lai 751 gold badge2 silver badges10 bronze badges1 Answer
Reset to default 4Found this via google and decided to answer in case others find it too.
Your example is not working, because React does not update the ponent when the promise resolves. This means that your image URL remains undefined
.
To fix this, you can call this.setState()
in the promise (or dispatch an action if you are using flux / redux). This will automatically update the state for you with the new URL.
Code example
const config = {
apiKey: "apiKey",
authDomain: "authDomain",
storageBucket: "bucketURL"
}
firebase.initializeApp(config)
const storage = firebase.storage().ref()
class HelloMessage extends React.Component {
constructor () {
super()
this.state = {
lithuania: '',
uk: ''
}
this.getImage('lithuania')
this.getImage('uk')
}
getImage (image) {
storage.child(`${image}.png`).getDownloadURL().then((url) => {
this.state[image] = url
this.setState(this.state)
})
}
render() {
return (
<div>
Hello Lithuania<br />
<img src={ this.state.lithuania } alt="Lithuanian flag" />
<br />
Hello United Kingdom<br />
<img src={ this.state.uk } alt="UK flag" />
</div>
);
}
}
Full codepen: https://codepen.io/DeividasK/pen/pRmbWq
本文标签: javascriptLoading Pictures from Firebase Storage to ReactStack Overflow
版权声明:本文标题:javascript - Loading Pictures from Firebase Storage to React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744191222a2594526.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论