I am using papaparse to implement a csv file in my react native application. I cant figure out why I keep getting this error that states Error parsing CSV file: [TypeError: Network request failed].
import React, { Component } from 'react';
import { Text, StyleSheet, TouchableOpacity, ScrollView, Alert } from 'react-native';
import Papa from 'papaparse';
class App extends Component {
constructor(props) {
super(props);
this.state = {
answers: {},
csvData: []
};
this.getData = this.getData.bind(this);
}
componentDidMount() {
this.parseCSV();
}
async parseCSV() {
try {
const response = await fetch('./assets/data.csv');
const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');
const result = await reader.read();
const csvData = decoder.decode(result.value);
Papa.parse(csvData, {
header: true,
complete: this.getData
});
} catch (error) {
console.error('Error parsing CSV file:', error);
}
}
getData(result) {
this.setState({ csvData: result.data });
}
New contributor
Kainyn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.