I am new with @pnp/sp and I am having problems with my code:
public render(): React.ReactElement<IVisualizadorPermisosProps> {
var arbol=[];
sp.web.get().then((data)=>{
arbol.push({name: data.Title, children:[]});
});
//Other code
return (
<div id="principal">
<Select options={opciones} onChange={this.onUserSelection} />
<div id="treeWrapper" style={{ width: '50em', height: '20em' }}>
<Tree data={arbol} />
</div>
</div>
);
}
My problem here is that executing this code, the code inside the sp.web.get()
block is not executing before the rest of the code, as I pretend. For example the return statement is executing before arbol.push({name: data.Title, children:[]});
so I get execution errors cause variable arbol
can't be empty. If I put a break point inside the "get" block, it reaches the break point in the middle of the execution of the other code.
This situation makes me believe that the "get" call is asynchronous.
Is that truth? What is the solution to my problem?
Thanks.
sp.web.get()
incomponentDidMount
method. – Ganesh Sanap Jul 26 '19 at 11:34