I’m having a problem with checking an optional string parameter of an object returned from a fetch call:
const uploadResponse = await fetch(uploadUrl, {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "video/mp4",
"Content-Length": String(videoBuffer.length),
},
body: videoBuffer,
});
const jobStatus = (await uploadResponse.json()) as AppBskyVideoDefs.JobStatus;
console.log(JSON.stringify(jobStatus, null, 4));
console.log(" error:", jobStatus.error);
if (jobStatus.error) {
console.warn(` Video job status: '${jobStatus.error}'. Video will be posted as a link`);
}
The above code, when run in a case where the error
field is populated generates the following output:
{
"jobStatus": {
"did": "",
"error": "daily_vid_limit_exceeded",
"jobId": "",
"state": ""
}
}
error: undefined
Somehow, despite the fact that the stringify of the object finds the error
element populated with a string, the direct access log of jobStatus.error
and the following test in the if statement think it’s undefined/false.
How is this possible and what can I do to properly check the error
parameter here?
I’ve tried changing the if condition to explicitly check for undefined as well as other cases. Nothing I’ve tried has resulted in any different outcome.