how to deal with object when while fetching data it is initially null and then it gets filled with data

react suspense data fetching
componentdidmount
react hooks
react state variable undefined
redux update state object
react useeffect
react state undefined after setstate
react props undefined in render

I have been trying to extract some information from the props this way.

let roleType=this.props.user.data.permissions.map((val)=>{

            console.log(val);
});

In the initial stage when the component is getting rendered data has nothing inside it so I get an error that it can't map over which is true. How do I deal with these cases. If I console log the above props I see that data is initially empty and then it gets filled. However, the web app crashes because of this?

In order to avoid cases when you try to deal with such scenarios, you either intialize the data in the format you expect it to be or you provide conditional checks to it while using

let roleType=this.props.user && this.props.user.data && this.props.user.data.permissions && this.props.user.data.permissions.map((val)=>{
        console.log(val);
});

or initialise it like

state = {
    user: {
         data: {
             permissions: []
         }
    }
}

What other thing that you can do to avoid unexpected undefined scenarios is to use PropType validation

Component.propTypes = {
    user: PropTypes.shape({
         data: PropTypes.shape({
             permissions: PropTypes.Array
         })
    })
}

One other improvement that you can do over the first method is to write a function that checks for nested data

function checkNested(obj, accessor) {
  var args = accessor.split('.')

  for (var i = 0; i < args.length; i++) {
    if (!obj || !obj.hasOwnProperty(args[i])) {
      return false;
    }
    obj = obj[args[i]];
  }
  return true;
}

and then you can use it like

let roleType= checkNested(this.props.user, 'data.permissions') && this.props.user.data.permissions.map((val)=>{
        console.log(val);
});

P.S. You can also improve the above method to take into consideration the array index

Watch Out for Undefined State, A component's state (e.g. this.state ) begins life as null . When you fetch data asynchronously, the component will render at are called before the initial render, asynchronous calls made there will not This is not the ideal way to handle it though. Get the data "soon" axios.get('/thedata').then(res => { this. From what I see you problem is not in the database connection, you have some untreated exceptions. You have a NULL pointer exception also in onCreate(Highscore.java:59)

Simply check to see if the object has the required attributes you're looking for before mapping through it. If it doesn't use an empty array.

let roleType = this.props.user.data ? this.props.user.data.permissions.map((val) => {
  console.log(val);
}) : [];

Suspense for Data Fetching (Experimental) – React, For example, if you're looking for a data fetching tutorial that works today, read this ProfilePage')); // Lazy-loaded // Show a spinner while the profile is loading trigger fetching anymore function ProfileTimeline({ posts }) { if (posts === null) object represents the data that isn't there yet, but might eventually get loaded. How to fetch data in React. Newcomers to React often start with applications that don't need data fetching at all. Usually they are confronted with Counter, Todo or TicTacToe applications. That's good, because data fetching adds another layer of complexity to your application while taking the first steps in React.

Look like you have to make an API call to get the data, so add that code for fetching the data in componentDidmount once the response came, set the data response in the state. So that component will render again with updated data.

Also, put a condition on the rendering section where going to use the state so that it won't be trying to access any property of a null value.

Pro ASP.NET 3.5 in C# 2008, To prevent this problem, you should check for null values or set default view state won't run into null references because it uses OnInit() to set initial view state values. However, if you need to use these objects to store or retrieve data, you can You can return a single serializable object from this method, which will be​  Strange thing though, when i log out, then re-login using OTHER facebook account, I can see the live feed and the profile page no problem. Everything works fine again. However, once i login using MY account, problem starts again.

EDIT: Like other people stated, the question doesn't really tell how you receive the data. My answer is based on you getting the data on load time. This answer doesn't work if you get the data externally or through an async call.

You can wait for the DOM Content to be fully loaded before running the code.

document.addEventListener('DOMContentLoaded', function() {
    let roleType=this.props.user.data.permissions.map((val)=>{
        console.log(val);
    });
}, false);

Another way to do this is by waiting for the page to be fully loaded. This includes images and styling.

window.onload = function(e){ 
    let roleType=this.props.user.data.permissions.map((val)=>{
        console.log(val);
    });
}

The first one is quicker in running, but you might be better of if you need the images to be loaded before running the code.

Finally it could also be a problem with the order of your code and simply placing your code lower might also fix the problem.

Fetch data dynamically, Using an HttpRequest object to load a file. The getString() method is good for an HTTP GET request that returns a string loaded from a resource. For other cases,  Humans don't really care how long it takes to fetch page 50 of a result set-- they're going to give up long before then. If your intention is to send the data to a Java process to process the data (this will be substantially less efficient than processing the data in the database-- Oracle and PL/SQL are designed specifically to process large

Python Crash Course: A Hands-On, Project-Based Introduction to , 'POST': # Initial request; pre-fill form with the current entry. --snip-- We retrieve the entry and the topic associated with this entry. We then check if the owner of the topic matches the currently logged-in user; if they don't match, we raise an Http404 exception. Now the topic has all the required data and will save successfully. The ideal solution is to fetch all data and code in parallel after navigating to a new set of routes. For good UX, we should also provide feedback on this loading state to users. In React Router v2 and v3, we can use the getComponent method on routes to load split-out code bundles in parallel.

Actions and reducers: updating state · Human Redux, Certainly, there will be some function you call that initiates fetching of data. Then, if the fetch was successful, you'll get data back from the server. Just like our application state is made up of plain objects, the actions are also plain objects. on a reducer that was handling that payload, then having to look it up, or worse,  Hi All, Thanks for your inputs The issue was resolved by fixing the Precision Mis-Match. We are using Datadirect ODBC driver to connection to MS SQL Server 2008(As Source and Lookup) on AIX 5.3 box, Informatica 8.6.1HF11.

Warning for: PropTypes.object.isRequired when prop is `null` · Issue , As PropTypes is about the 'type' of prop, null is an empty object, but still a type of to host and review code, manage projects, and build software together. The touble is that the isRequired gets evaluated FIRST and it will never to the user if data couldn't be fetched), which is null when the data loaded  That data looks so fetching on you: Understanding the JS Fetch API. fetch() is not the only way JS can deal with data from servers, but it might be the easiest. The go-to method used to be XMLHttpRequest, however that method is starting to die off.

Comments
  • depends on how data is populated .. at a guess, it's asynchronous behaviour that you don't know how to deal with - but you haven't shown nearly enough code to understand your problem