I am having this error where the user is allowed to upload a document but the first time when it's chosen it causes an error but if the user does it the second time, the document is uploaded successfully.
The function first called it PickDocument btw.
The error I'm getting is the alert from FilePArser function
this is my related code:
const [userOut, setUserOut] = useState("")
const pickDocument = async () => {
console.log('pic Doc')
try {
let input = await DocumentPicker.getDocumentAsync({
type: "text/plain",
})
setUserOut(await FileSystem.readAsStringAsync(input.uri))
createLoans()
} catch (error) {
console.log(error);
}
}
const fileParser = () => {
console.log('file parser')
const parsedLoans = [];
var newUserOut = userOut;
if (newUserOut.length == 0) {
Alert.alert('wrong document','Try Again')
return;
}
//remove the grants
var grantPos = newUserOut.search("Grant Type:");
var pos = newUserOut.search("Loan Type:");
//hopefully just the loans now
newUserOut = newUserOut.slice(pos, grantPos)
while (newUserOut.length > 0) {
var lastPos = newUserOut.lastIndexOf("Loan Type:");
parsedLoans.push(newUserOut.slice(lastPos, newUserOut.length));
newUserOut = newUserOut.slice(0, lastPos);
}
//console.log('parsed loans: ' + parsedLoans)
return parsedLoans;
};
const createLoans = () => {
console.log('create loans')
const newLoans = fileParser();
const title= 'Loan Amount:$'
const interest = 'Loan Interest Rate:'
for(let i =1; i <= newLoans.length; i++){
let loan = newLoans[i]
let goalTitle=loan.substring(loan.indexOf(title)+title.length,loan.indexOf('Loan Disbursed Amount:'))
//console.log("goalTitle: " + goalTitle)
let interestRate = loan.substring(loan.indexOf(interest)+interest.length,loan.indexOf('Loan Repayment Plan Type')-1)
//console.log("Interest rate: "+ interestRate)
let years = 10
let paidOff = 50
setGoalCounter(goalCounter+1)
let id =i
addGoalHandler(goalTitle,interestRate,years,paidOff,id)
}
return
};