I am getting an error:
[SyntaxError: JSON Parse error: Unexpected identifier "undefined"]
I am trying to do 2 API calls because one depends on the other.
Code:
const bearer = "Bearer " + user?.token;
fetch(`${API}/getDocumentById`, {
method: "POST",
headers: {
Authorization: bearer,
"X-Requested-With": "XMLHttpRequest",
"Content-Type": "application/json",
},
body: JSON.stringify({ document_id: doc.id }),
})
.then((response) => response.json())
.then((json) => {
console.log(`${API}/viewFile?file=${json.document.file}`);
// This console logs returns a good value:
// http://my-api.com/api/viewFile?file=1620802187_MAPPA-definitiva.pdf
fetch(`${API}/viewFile?file=${json.document.file}`, {
method: "GET",
headers: {
Authorization: bearer,
"X-Requested-With": "XMLHttpRequest",
},
})
.then((response) => response.json())
.then((json) => {
setImage(json.document);
})
.catch((viewFileError) => {
// HERE IS WHERE IT THROWS THE ERROR
console.log({ viewFileError });
});
setData(json.document);
})
.catch((err) => {
setLoading(false);
console.log({ err });
});
On Postman it works properly.
What am I doing wrong?