0

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:

  1. 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);
     }
   }
  1. Then I want to save the retrieved value in my state (counterID) variable:

    export interface IListItemsState{
      counterItem: ICounterItem;
      counterID: number
      }
    
  2. 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)

Counter numeber

 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

1
  • Try using console.log(item[0].NewId);. Let me know if it works. Commented Aug 1, 2023 at 11:52

1 Answer 1

2

You are receiving array of objects as a result from your SharePoint API call. So, you will have to get the NewId from first element/object of array result.

Try using this:

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);
    }
}

private getCounterValue(): void {
    this._spDataProvider.ReadCounterValue("Counter").then((items)=>{  
        console.log("Counter Value: "+ JSON.stringify(items))
        console.log(items[0].NewId);
        this.setState({
            counterID: items[0].NewId
        })
    })
}
6
  • Hi! It's not working. I get the error messagt that the type is of any and it could not find the index of 0.
    – Matthias
    Commented Aug 1, 2023 at 12:04
  • now it's working. I needed to define an array in the ReadCounterValue: so Promise<ICounterItem[]> and ICounterItem[]
    – Matthias
    Commented Aug 1, 2023 at 12:15
  • Yes, correct. Because your API call returns the array of objects. I have updated the answer with full example. Commented Aug 1, 2023 at 12:17
  • 1
    thank you very much!
    – Matthias
    Commented Aug 1, 2023 at 12:40
  • You're welcome, glad it helped you! Commented Aug 1, 2023 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.