嗨,當我登錄到瀏覽器控制臺時,我有如下資料,
data **** {progressValue: {...}}
看起來像下面
progress {
completed: "10",
total: "20",
}
我想檢查完成值是否大于或等于總值然后停止輪詢。
下面是代碼,
I have defined progress type for query like so
type progress {
completed: String
total: String
}
async Progress(id:
QueryCheckArgs['id']) {
const { data } = await this.get(
`/${BASE_PATH}/${id}/progress/`
);
const output = {
completed: data.connectivity_test_completed_if,
total: data.connectivity_test_total_if,
};
return output;
}
const Component = () => {
const {
data: progressValue,
startPolling: startPolling,
} = useProgress({
variables: { id },
fetchPolicy: 'network-only',
});
if (progressValue) {
if (progressValue.progress.completed >=
progressValue.progress.total) { //error here says object
//is
//possibly null or undefined
setProgress(undefined);
stopPolling();
}
}
}
我怎樣才能重寫上面的條件,這樣錯誤物件可能是未定義的或 null 是固定的。現在應該是什么型別的進展,謝謝。
uj5u.com熱心網友回復:
由于您尚未定義任何資料型別,因此請嘗試使用Object Chaining:
if (progressValue?.progress?.completed >= progressValue?.progress?.total)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/403101.html
標籤:
