我有一個:
<img src={ "http://127.0.0.1:5001/api/users/profile_photo/" user.profile_photo }
className="img-fluid rounded-circle"
alt="rounded image"/>
每當我在頁面中加載時,該函式getUser()都會運行并檢索用戶。
const getUser = async () => {
let user = await axiosWithAuth().get('/users/' userId);
setUser(user.data);
};
user.profile_photo是我從 AWS s3 取回的關鍵。前任。43256722324654
問題:當頁面加載時,影像 src 尚未設定為它的樣子,因為在 express 后端,profile_photo 來自未定義。它在第二次回圈時被定義。
因此,AWS 會拋出一個錯誤 noSuchKey
/Users/timbogdanov/Desktop/hlam/server/node_modules/AWS-SDK/lib/request.js:31 拋出錯誤;^
NoSuchKey:指定的鍵不存在。
在設定用戶狀態物件之前,有什么方法可以阻止影像發送獲取請求?
謝謝!
uj5u.com熱心網友回復:
添加條件以渲染影像。如果user.profile_photo未定義,則不渲染它。
{user.profile_photo && <img src={ "http://127.0.0.1:5001/api/users/profile_photo/" user.profile_photo }
className="img-fluid rounded-circle"
alt="rounded image"/>
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/412503.html
標籤:
