Javascript Typescript:异步等待承诺有问题。所有结果都循环
由于此结果集的类型未知,因此存在问题。我尝试过几种不同的方法,但总是出现同样的错误,不知道该怎么办。谢谢你的帮助 这是错误消息-Javascript Typescript:异步等待承诺有问题。所有结果都循环,javascript,node.js,reactjs,typescript,promise,Javascript,Node.js,Reactjs,Typescript,Promise,由于此结果集的类型未知,因此存在问题。我尝试过几种不同的方法,但总是出现同样的错误,不知道该怎么办。谢谢你的帮助 这是错误消息- 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
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.
TypeError:无法读取未定义的属性“props”
在C:\TS\mytask\src\helloworld.js:153:47
在步骤(C:\TS\mytask\src\helloworld.js:33:23)
在Object.next(C:\TS\mytask\src\helloworld.js:14:53)
在C:\TS\mytask\src\helloworld.js:8:71
在新的承诺()
在等待者(C:\TS\mytask\src\helloworld.js:4:12)
在C:\TS\mytask\src\helloworld.js:144:60
在新的承诺()
在searchUsers上(C:\TS\mytask\src\helloworld.js:144:12)
反对。(C:\TS\mytask\src\helloworld.js:173:13)
承诺{[]}
(节点:37216)未处理的PromisejectionWarning:[对象数组]
(使用`node--trace warnings…`显示警告的创建位置)
(节点:37216)未处理的PromisejectionWarning:未处理的承诺拒绝。此错误源于在没有catch块的异步函数中抛出,或者拒绝未使用.catch()处理的承诺。要在未处理的承诺拒绝时终止节点进程,请使用CLI标志“---unhandled rejections=strict”(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (拒绝id:1)
(节点:37216)[DEP0018]弃用警告:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将使用非零退出代码终止Node.js进程。
代码-
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);
}
});
}
function searchUsers(filterText:string):IPersonaProps[]| Promise{
返回新承诺(异步(解析:任意,拒绝:任意)=>{
让人们:IPersonaProps[]=[];
试试{
const tempeople=wait this.props.context.webAPI.retrieveMultipleRecords(“系统用户?$select=fullname,internalemailaddress,systemuserid”);
等待承诺.all(tempPeople.entities.map)((entity:any)=>{
push({“text”:entity.fullname,“secondaryText”:entity.internalemailaddress,“id”:entity.systemuserid});//如果值不同,则更改字段名
}));
决心(人);
}
捕捉(错误){
控制台日志(err);
拒绝(人);
}
});
}
目前,对于1个结果来说,这很好,但是当我尝试返回一个结果集合并循环它们以推送到People数组时,我总是得到相同的错误
再次感谢 这似乎是您已声明但尚未初始化的类的成员函数,因此
此
未定义。要确认,请添加如何使用您的类的代码
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);
}
});
}
private\u searchUsers(filterText:string):IPersonaProps[]承诺{
返回新承诺(异步(解析:任意,拒绝:任意)=>{
让人们:IPersonaProps[]=[];
//这是对类的引用
const tempeople=wait this.props.context.webAPI.retrieveMultipleRecords(“系统用户?$select=fullname,internalemailaddress,systemuserid”);
//需要使回调函数异步,以便它成为一个承诺数组
等待Promise.all(tempPeople.entities.map)(异步(实体:any)=>{
push({“text”:entity.fullname,“secondaryText”:entity.internalemailaddress,“id”:entity.systemuserid});//如果值不同,则更改字段名
}));
决心(人);
}
捕捉(错误){
控制台日志(err);
拒绝(人);
}
});
}
我发现了问题。下面是我如何让它工作的。谢谢你的帮助
private _searchUsers(filterText: string): IPersonaProps[] | Promise<IPersonaProps[]> {
return new Promise(async (resolve: any, reject: any) => {
let People: IPersonaProps[] = [];
const config = new WebApiConfig("9.1");
const options: string = "?$select=fullname,internalemailaddress,systemuserid";
retrieveMultiple(config, "systemusers", options)
.then(
(results) => {
const People: IPersonaProps[] = [];
for (const record of results.value) {
People.push({ "text": record.fullname, "secondaryText": record.internalemailaddress, "id" : record.systemuserid });
resolve(People);
}
},
(error) => {
reject(People);
console.log(error);
}
);
});
};
private\u searchUsers(filterText:string):IPersonaProps[]承诺{
返回新承诺(异步(解析:任意,拒绝:任意)=>{
让人们:IPersonaProps[]=[];
const config=new-WebApiConfig(“9.1”);
常量选项:string=“?$select=fullname,internalemailaddress,systemuserid”;
retrieveMultiple(配置,“系统用户”,选项)
.那么(
(结果)=>{
const People:IPersonaProps[]=[];
对于(results.value的常量记录){
push({“text”:record.fullname,“secondaryText”:record.internalemailaddress,“id”:record.systemuserid});
决心(人);
}
},
(错误)=>{
拒绝(人);
console.log(错误);
}
);
});
};
代码的格式是否正确。粘贴到编辑器时出错。}catch(err){console.log(err);reject(People);}
打开的位置在哪里{
为此?错过了开头的尝试。抱歉-不得不从代码中删除一些不需要共享的其他内容。因此,我们试图将其剥离到核心问题。您的映射函数应该返回实体
…好的,更新了代码和错误消息-对此表示抱歉。正如我所说,我正在尝试将其剥离到基本问题他越来越生气了。