Pnpjs: Call two REST

Created on 25 Mar 2019  路  5Comments  路  Source: pnp/pnpjs

Category

  • [ ] Enhancement
  • [x] Bug
  • [x] Question
  • [ ] Documentation gap/issue

Code sample

` EXAMPLE CODE >>
sp.web.lists.getByTitle(listname).items.select(select).expand("AssignedTo").top(1000).get().then( res => {

      if (res.length > 0) {
        this.setState({
          height: res.length * this.rowHeight + 70
        });
      }

      // MAP TASK ITEMS
      const data: IGanttData[] = res.map( obj => {

        // COLLECT PROJECT VALUES, LOOKUPVALUES
        **let proj = sp.web.lists.getByTitle("Projects").items.getById(obj.ProjectId).get();
        console.log(proj);**

        let users = "";
        if ( obj.hasOwnProperty("AssignedTo") ) {
          users = obj.AssignedTo[0].EMail;
        }

        let rObj: IGanttData = {
          id: obj.ID,
          text: obj.Title,
          Body: obj.Body,
          start_date: moment(obj.StartDate).format("DD/MM/YYYY"),
          end_date: moment(obj.DueDate).format("DD/MM/YYYY"),
          progress: obj.PercentComplete,
          parent: obj.PredecessorsId[obj.PredecessorsId.length-1],
          users: users || "",
          open: true,
          sendmail: true
        };
        return rObj;
      });

}

`

Expected / Desired Behavior / Question

get proj property values from ListItem

Observed Behavior

No proj propery values returned. The code does not return need values from
let proj = sp.web.lists.getByTitle("Projects").items.getById(obj.ProjectId).get();

Steps to Reproduce

Ref. sample code

Submission Guidelines

Thank you for your feedback!

non-library answered question

Most helpful comment

Also, for async/await to work in a loop for/of loops (this is important) should be used instead of .map.
However, the code in the sample introduces a large issue. A potential number of requests could be 1000+1, this is huge. The client-side app can fail and SPO (if it's SPO) can throttle and block API, once again leading to an unworking app.
It's better to expand lookup values or request complete project list's items (if unexpandable fields are needed) and merge 2 arrays in memory rather than requesting each project details separately.

All 5 comments

Do you see any errors in the console? Can you look at the call in the Network tab to see whether it is successful, even if it returns nothing?

I think your issue is probably that you are expecting proj to have a value before the async call completes.

You either need to await the result or use then after the get() call. In your case proj is a Promise. You are also using "proj" and "obj" - are those meant to be the same variable?

Also, for async/await to work in a loop for/of loops (this is important) should be used instead of .map.
However, the code in the sample introduces a large issue. A potential number of requests could be 1000+1, this is huge. The client-side app can fail and SPO (if it's SPO) can throttle and block API, once again leading to an unworking app.
It's better to expand lookup values or request complete project list's items (if unexpandable fields are needed) and merge 2 arrays in memory rather than requesting each project details separately.

Going to close this as answered. Thanks!

Thanks for all your reply comments, I will check issue further and try load two arrays in memory.

Was this page helpful?
0 / 5 - 0 ratings