我試圖從Firebase存儲中檢索圖片網址,然後使用該網址設置圖片。然而,似乎是我設置的SRC爲不確定的值與我當前的代碼:從Firebase存儲中加載圖片以進行反應
這是我的功能我用從火力地堡存儲檢索
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);
});
}
我把它從一個組件作出反應使用這樣的功能:
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}
/>
);
}
由於ProfilePictureUrl返回undefined,圖像未正確加載。
我也用試圖使測試儀內渲染()這樣的:
render() {
if(profilePictureUrl !== undefined) {
console.log("defined");
}
else {
console.log("undefined");
}
// returns 'undefined'
}
而且我被記錄表明該函數返回一個未定義的值別的反應。我懷疑它與Firebase的異步本質有關,但我不知道如何解決它。通過谷歌React-Native: Download Image from Firebase Storage