英文:
New Promise Data is Undefined after Data Transformation
问题
所以我必须在dbObject和UI期望的内容之间进行数据转换。我决定将其翻译成正确的对象类型并创建一个包含正确数据类型数组的新承诺,然后传递它。
但出现了一个问题,当它到达UI层时,承诺是正常的,但在内容上却给我一个未定义的错误。
这是读取第一个承诺、翻译对象并创建包含新对象数组的新承诺的方法:
public async GetAllDebugEvents(): Promise<DebugEventObject[]> {
let debugEvents: DebugEventObject[] = [];
if (this.properties.debugMode){
await this.data.GetAllDebugEvents().then((items: any[]) =>{
items.forEach((item: any) => {
debugEvents.push(this.TranslateDBObjectToDebugEventObject(item, new DebugEventObject()));
});
let p = new Promise<DebugEventObject[]>((resolve) => { resolve(debugEvents);});
return p;
});
}
else {
return null;
}
}
这是然后使用该新承诺的方法:
private renderDebug(): void {
let debugHtml: string = '';
debugHtml += `<div id="debugContainer" class="${styles.debugContainer}">`;
this.logic.GetAllDebugEvents().then((events: DebugEventObject[]) =>{
events.forEach((e: DebugEventObject) => {
debugHtml +=`<div class="${styles.row}"><div class="${styles.columnFull}">[${e.MessageDate}] - ${e.ErrorMessage.ErrorMessage} [${e.ErrorMessage.StackTrace}]</div></div>`
});
debugHtml += `</div>`;
}).then(()=>{
JQ("#WebPartBase").append(debugHtml);
});
}
现在当执行then
时,我得到一个 events is undefined 错误。现在,当在第一个函数中创建承诺时,debugEvents数组大约有48个项目,所以我知道这不是数据问题。
我可能只是漏掉了一些明显的东西,但当你一直盯着它看了一段时间,尝试了各种不同的解决方案和变通方法后,除非你需要其他的眼睛。
英文:
So I have to do a data transformation between the dbObject and what the UI is expecting. I decided to just translate it into the correct object types and create a new promise with an array of the correct datatype and pass it on.
For some reason when it gets to the UI layer though, the promise is fine but it gives me an undefined error on the content.
Here's the method that reads the first promise, translate the objects and creates the new promise containing the array of new objects.
public async GetAllDebugEvents(): Promise<DebugEventObject[]> {
let debugEvents: DebugEventObject[] = [];
if (this.properties.debugMode){
await this.data.GetAllDebugEvents().then((items: any[]) =>{
items.forEach((item: any) => {
debugEvents.push(this.TranslateDBObjectToDebugEventObject(item, new DebugEventObject()));
});
let p = new Promise<DebugEventObject[]>((resolve) => { resolve(debugEvents);});
return p;
});
}
else {
return null;
}
}
Here is the method that then utilizes that new promise:
private renderDebug(): void {
let debugHtml: string = '';
debugHtml += `<div id="debugContainer" class="${styles.debugContainer}">`;
this.logic.GetAllDebugEvents().then((events: DebugEventObject[]) =>{
events.forEach((e: DebugEventObject) => {
debugHtml +=`<div class="${styles.row}"><div class="${styles.columnFull}">[${e.MessageDate}] - ${e.ErrorMessage.ErrorMessage} [${e.ErrorMessage.StackTrace}]</div></div>`
});
debugHtml += `</div>`;
}).then(()=>{
JQ("#WebPartBase").append(debugHtml);
});
}
Now I'm getting an events is undefined error when the then is executed. Now the debugEvents array has about 48 items in it when the promise is created in the first function, so I know it's not a data issue.
I'm probably just missing something obvious but when you've been staring at it for a while and trying various different solutions and work arounds you can only go so far before you need those other sets of eyes.
答案1
得分: 0
在这一行:
await this.data.GetAllDebugEvents().then((items: any[]) => {
你缺少了一个返回语句,所以你的 promise 从未返回结果。尝试将 await
更改为 return
。
你还不需要这一行 - 你正在创建一个立即解决的新 promise,这是没有意义的,因为你的函数已经返回一个 promise。只需返回debugEvents
即可。
let p = new Promise<DebugEventObject[]>((resolve) => { resolve(debugEvents);});
英文:
On this line:
await this.data.GetAllDebugEvents().then((items: any[]) =>{
You're missing a return statement so your promise never returns the result. Try changing the await
to return
.
You also don't need this line - you're creating a new promise which immediately resolves, which is pointless because your function already returns a promise. Just return debugEvents
instead.
let p = new Promise<DebugEventObject[]>((resolve) => { resolve(debugEvents);});
答案2
得分: 0
I would suggest to remove await because you are handling the value in the .then() function and also you are returning a promise from inside another promise but you are not returning the parent promise, that is why it is undefined. I simplified the function code to be more concise to use async await.
public async GetAllDebugEvents(): Promise<DebugEventObject[]> {
let debugEvents: DebugEventObject[] = [];
if (this.properties.debugMode){
const items = await this.data.GetAllDebugEvents();
items.forEach((item: any) => {
debugEvents.push(this.TranslateDBObjectToDebugEventObject(item, new DebugEventObject()));
});
return debugEvents;
}
else {
return null;
}
}
英文:
I would suggest to remove await because you are handling the value in the .then() function and also you are returning a promise from inside another promise but you are not returning the parent promise, that is why it is undefined. I simplified the function code to be more concise to use async await.
public async GetAllDebugEvents(): Promise<DebugEventObject[]> {
let debugEvents: DebugEventObject[] = [];
if (this.properties.debugMode){
const items = await this.data.GetAllDebugEvents();
items.forEach((item: any) => {
debugEvents.push(this.TranslateDBObjectToDebugEventObject(item, new DebugEventObject()));
});
return debugEvents;
}
else {
return null;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论