首页 > 解决方案 > 打字稿:异步等待 promise.all 循环所有结果的问题

问题描述

由于类型未知,此结果集存在问题。我尝试了几种不同的方法,但不断收到相同的错误,不知道该怎么做。谢谢你的帮助!

这是错误消息 -

TypeError: Cannot read property 'props' of undefined
    at C:\TS\mytask\src\helloworld.js:153:47
    at step (C:\TS\mytask\src\helloworld.js:33:23)
    at Object.next (C:\TS\mytask\src\helloworld.js:14:53)
    at C:\TS\mytask\src\helloworld.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (C:\TS\mytask\src\helloworld.js:4:12)
    at C:\TS\mytask\src\helloworld.js:144:60
    at new Promise (<anonymous>)
    at searchUsers (C:\TS\mytask\src\helloworld.js:144:12)
    at Object.<anonymous> (C:\TS\mytask\src\helloworld.js:173:13)
Promise { <rejected> [] }
(node:37216) UnhandledPromiseRejectionWarning: [object Array]
(Use `node --trace-warnings ...` to show where the warning was created)
(node:37216) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:37216) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

代码 -

    function searchUsers(filterText: string): IPersonaProps[] | Promise<IPersonaProps[]> {
    return new Promise(async (resolve: any, reject: any) => {
       let People: IPersonaProps[] = [];
       try { 
          const tempPeople = await this.props.context.webAPI.retrieveMultipleRecords("systemuser?$select=fullname,internalemailaddress,systemuserid");

          await Promise.all(tempPeople.entities.map((entity: any ) =>{
          People.push({ "text": entity.fullname, "secondaryText": entity.internalemailaddress, "id" : entity.systemuserid }); //change fieldname if values are different           
          }));
          resolve(People);
      }
      catch (err) {
        console.log(err);
        reject(People);
      }
    });
   }

目前这适用于 1 个结果,但是当我尝试取回一组结果并循环遍历它们以推送到 People 数组时,我总是会收到相同的错误。

再次感谢!

标签: javascriptnode.jsreactjstypescriptpromise

解决方案


看起来这是您已声明但尚未初始化的类的成员函数,因此this未定义。要确认,请添加您如何使用课程的代码...

private _searchUsers(filterText: string): IPersonaProps[] | Promise<IPersonaProps[]> {
    return new Promise(async (resolve: any, reject: any) => {
      let People: IPersonaProps[] = [];
      
      // This is a reference to a class
      const tempPeople = await this.props.context.webAPI.retrieveMultipleRecords("systemuser?$select=fullname,internalemailaddress,systemuserid");

      // Need to make the callback function async so that it becomes an array of promises
      await Promise.all(tempPeople.entities.map(async (entity: any ) => {
        People.push({ "text": entity.fullname, "secondaryText": entity.internalemailaddress, "id" : entity.systemuserid }); //change fieldname if values are different           
      }));
      resolve(People);
    }
    catch (err) {
      console.log(err);
      reject(People);
    }
  });
}

推荐阅读