I have a lack of understanding of retrieving a value from a list. I want to use it to build a counter and then increment the counter locally after a button is pressed. After the incrementation I want to write it back to the counter list.
The value is retrieved by using PNP/JS. This works so far. Everytime I want to write it to local variable I get the error message "undefined". I don't know where the problem is coming from and where I need to defined the type.
Here are my code steps so far:
- First I filter the element with the type ICounterItem:
My defined Item and the retrieving of the specific counter Value. The number should be 2
export interface ICounterItem{
Id: number,
NewId: number
}
public ReadCounterValue = async (listTitle: string): Promise<ICounterItem> =>{
try{
const response: ICounterItem= await this._sp.web.lists
.getByTitle(listTitle)
.items.top(1).filter("Title eq 'TO'")
.select("Id","NewId")()
return response;
}
catch(e){
console.log(e);
}
}
Then I want to save the retrieved value in my state (counterID) variable:
export interface IListItemsState{ counterItem: ICounterItem; counterID: number }
I wrote the constructor and intialize the counterID in the next step with 0
constructor(props:ISpTestingfilesystemProps, state: IListItemsState){ super(props); this.state={ counterID:0 } this._spDataProvider= new SharePointDataProvider(); this._sp= getSP(); }
Then I try to set the value to counterID. The Counter ID should be 2 (See the screenshot)
private getCounterValue(): void {
this._spDataProvider.ReadCounterValue("Counter").then((item)=>{
console.log("Counter Value: "+ JSON.stringify(item))
console.log(item.NewId);
this.setState({
counterID: item.NewId
})
})
}
When I lock it to the console and try to put it in a div. I get blank and in the console undefined.
I hope you can help me with finding my error.
Best regards
console.log(item[0].NewId);
. Let me know if it works.