0

Getting data from SPFX React (json) -

this.state = {
      testProperty1: "",
      hideDialog: true,
      showModal: false,
      items: [
        {
          Id: "",
          title: "",
          updated: "",
          FileLeafRef: ""
        } as IItem
      ] as IItem[]
    } as IReactDocumentLibraryState;

 pnp.sp.web.lists
      .getByTitle("Student%20Details%20No%20Content%20Type")
      .items.select(
        "Id",
        "Title",
        "FileLeafRef",
        "Student_x0020_Class",
        "Created"
      )
      .top(50)
      .orderBy("Modified", true)
      .get()
      .then((_items: any[]) => {
        console.log(_items);

        this.setState({ items: _items, testProperty1: "test data 2" });
      });

Got the data from console.log from browser -

    (2) [{…}, {…}]
0:
Created: "2019-01-29T08:46:07Z"
FileLeafRef: "pnp.JPG"
ID: 4
Id: 4
Student_x0020_Class: "Class #1"
Title: "Title 1"
odata.editLink: "Web/Lists(guid'fa2e74fc-2c1c-4980-ae46-88cc856df7c1')/Items(4)"
odata.etag: ""3""
odata.id: "6b7072b4-b0b6-4933-800e-cf850a503bad"
odata.type: "SP.Data.Student_x0020_Details_x0020_No_x0020_Content_x0020_TypeItem"
__proto__: Object
1: {odata.type: "SP.Data.Student_x0020_Details_x0020_No_x0020_Content_x0020_TypeItem", odata.id: "1022114e-ab80-4cb3-a07c-39d433ea18c3", odata.etag: ""3"", odata.editLink: "Web/Lists(guid'fa2e74fc-2c1c-4980-ae46-88cc856df7c1')/Items(3)", Id: 3, …}
length: 2

**Render in SPFX react - **

{this.state.items.map(myitems1 => {
          //if (myitems1.Id != "") {
          return (
            <div key={myitems1.Id}>
              <span>{myitems1.title}</span>
              <div />
            </div>
          );
          // }
        })}

Error getting -

message: "Cannot read property 'state' of undefined" stack: "TypeError: Cannot read property 'state' of undefined↵ at eval (eval at ()↵ )"

I think the object is not gets parsed .

_items[0]
{odata.type: "SP.Data.Student_x0020_Details_x0020_No_x0020_Content_x0020_TypeItem", odata.id: "0a8a5f9e-a1b3-4a0f-87ad-e9c2a7e1f9d8", odata.etag: ""3"", odata.editLink: 

I am not getting data in render block ( How to check the state in render block ? ).

Please help .

  • Seems like you are not referencing this correctly. If the error is "Cannot read property 'state' of 'undefined'" then at the point where you are trying to access this.state, this is undefined. – Dylan Cristy Jan 29 at 15:58
  • I fixed it by changing the Interface .. property . The property cumming from online not matching with interface ... typecasting error . Thanks . – BISWANATH HALDER Jan 30 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.