简体   繁体   English

尝试通过键访问值时,Typescript字典对象返回未定义的

[英]Typescript dictionary object returning undefined when trying to access value by key

In my angular 7 app I'm trying to pass a dictionary object of type {[key : string] : string} from my service to my component. 在我的angular 7应用程序中,我试图将{[key : string] : string}类型的字典对象从服务传递到组件。

When I console.log the object, the console successfully returns the dictionary: {image : blob:http//..., model: blob:http//...} 当我console.log对象时,控制台成功返回字典: {image : blob:http//..., model: blob:http//...}

But when I try to access the image value like so: taskList['image'] it returns undefined ; 但是,当我尝试像这样访问image值时: taskList['image']它返回undefined which doesnt make any sense. 这没有任何意义。 Here's the code: 这是代码:

service: 服务:

public resolveTasks(callback : Function){
        forkJoin(...this.tasks).subscribe(async results => {
            let refMap : {[key: string] : string} = {};
            await results.forEach(async ref => {
                ref = await this.makeRequest("GET", ref);
                if(ref.type.includes('image')){
                    ref = URL.createObjectURL(ref); //create a url for the downloaded blob : blob:http://....
                    refMap['image'] = ref;
                } else {
                    const id = Math.floor(1000 + Math.random() * 9000); //add random number identifier
                    ref = URL.createObjectURL(this.blobToFile(ref, `model${id}.obj`))
                    refMap['model'] = ref
                }
            });
            callback(refMap); //{image:... , model: ....} loadContent is called here
            this.tasks = []; //empty the task list
        })
    }

component: 零件:

public ngOnInit() : void {
        //setup
        this.firebaseModel.resolveTasks(this.loadContent.bind(this));// pass loadContent as callback
    }

private loadContent(taskList : {[key:string] : string}) : void {
        console.log(taskList['image']) //trying to access blob by key, returns undefined
        const model : any = taskList['model'];
        const textureLoader = new THREE.TextureLoader(this.manager);
        const texture : string = textureLoader.load(taskList['image']);
        this.loadResources(model, texture, this.scene, this.renderer, this.container);
        this.animate();
    }

The issue was in my service : 问题出在我的service

This is the working code: 这是工作代码:

await Promise.all(results.map(async (ref) =>{
                ref = await this.makeRequest("GET", ref);
                if(ref.type.includes('image')){
                    ref = URL.createObjectURL(ref);
                    refMap['image'] = ref;
                } else {
                    const id = Math.floor(1000 + Math.random() * 9000); //add random number identifier
                    ref = URL.createObjectURL(this.blobToFile(ref, `model${id}.obj`))
                    refMap['model'] = ref
                }

            }));

The solution was found here, turns out that a forEach loop simply fires out multiple asynchronous calls instead of processing each get request sequentially; 在这里找到了解决方案,结果是forEach循环只是触发了多个异步调用,而不是顺序地处理每个get请求。 this post was very useful: 这篇文章非常有用:

Using async/await with a forEach loop 在forEach循环中使用异步/等待

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript - 如何通过键访问字典值? - TypeScript - How to access dictionary value by key? 尝试访问TypeScript词典时出现“不支持的索引器” - “Unsupported indexer” when trying to access a TypeScript dictionary Typescript 返回具有特定键值的 object 或 null - Typescript returning an object with specific key value or null 尝试从 TypeScript/React 中的提供者访问上下文时获取“未定义”值 - Getting an 'undefined' value when trying to access context from provider in TypeScript/React 返回新的 object,其中包含 typescript 中选定的键值对 - Returning new object containing selected key value pairs in typescript 基于Typescript中的参数返回具有键值对的对象 - Returning object with key value pairs based on arguments in Typescript 告诉打字稿没关系,键不在对象中,我希望它是未定义的或值 - Tell typescript its ok that key is not in object, I expect it to be undefined or the value TypeScript:访问 object 上的未定义键时返回的类型是错误的? - TypeScript: Returned type when accessing undefined key on object is wrong? 如何从打字稿中的对象值访问键 - How to access key from its value of object in typescript 如何使用 typescript 无法识别的映射值从 object 访问密钥? - How to access a key from an object using a mapped value that it is not recognized by typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM