I'm a beginner in React Native and Java Script development, and I'm facing an issue while trying to retrieve a JSON object and display it on the client side. I keep getting a "cannot read property of undefined" error when using websockets instead of "Fetch". I've tried debugging the code and confirmed that the JSON is being received from the server. Here's the relevant client-side code:
fetchDataWithSockets()
{
var url='ws://localhost:3000/';
var ws = new WebSocket( url );
var object;
ws.onmessage = (e) => {
object = JSON.parse(e);
}
this.setState({
dataSource: this.state.dataSource.cloneWithRows(object.movies),
isLoading: false,
empty: false,
documents: object.movies
});
}
Object on Server:
var object = {
"title": "The Basics - Networking",
"description": "Your app fetched this from a remote endpoint!",
"movies": [
{ "title": "Star Wars", "releaseYear": "1977"},
{ "title": "Back to the Future", "releaseYear": "1985"},
{ "title": "The Matrix", "releaseYear": "1999"},
{ "title": "Inception", "releaseYear": "2010"},
{ "title": "Interstellar", "releaseYear": "2014"}
]
}
If anyone could provide some assistance, I would greatly appreciate it :)