I am trying to upload GPX files using expo document picker. This code was working last week but is now returning undefined for result.type. The logged result object looks like this:
Object {
"assets": Array [
Object {
"mimeType": null,
"name": "marl-lake.gpx",
"size": 25435,
"uri": "file:///var/mobile/Containers/Data/Application/7FA283BA-7762-436D-972E-7EBF0A62D248/Library/Caches/ExponentExperienceData/%2540trailcollectivadmin%252Ftrailcollectiv/DocumentPicker/CDEAB533-970E-4162-BF01-F7366F56DC37.gpx",
},
],
"canceled": false,
}
const handleFileUpload = async () => {
try {
const result = await DocumentPicker.getDocumentAsync({
// type: 'application/gpx+xml', // You can specify the file type here.
});
console.log(result)
if (result.type === "success" && result.uri) {
closeModal();
uploadingFile(true)
const { uri } = result;
const fileContents = await readFileContents(uri);
const response = await http.postGpxFile(token, fileContents);
console.log("Successful upload of gpx contents", response.data);
navigation.navigate("GpxFileScreen", {params: { trailInfo: response.data }});
}
else {
throw "GPX upload error."
}
} catch (error) {
Alert.alert("An error occurred while uploading your GPX file:", error?.error?.message.toString() || error.toString())
console.error(error)
}
uploadingFile(false);
};
I have tried accessing uri in this new object using result.assets[0].uri but then the readFileContents throws a network request failed
const readFileContents = async (fileUri) => {
try {
const fileContents = await fetch(fileUri).then((response) =>
response.text()
);
return fileContents;
} catch (error) {
throw new Error("Error reading file contents: " + error.message);
}
};