简体   繁体   English

类型错误:无法读取未定义的属性(读取“id”)

[英]TypeError: Cannot read properties of undefined (reading 'id')

I have this error in my terminal:我的终端出现这个错误:

TypeError: Cannot read properties of undefined (reading 'id')类型错误:无法读取未定义的属性(读取“id”)

I'm trying to test the call to an API, but the error appears.我正在尝试测试对 API 的调用,但出现错误。

My function:我的function:

itemToForm = () => {
    this.api.send(this.component, 'get',
        { lang: 'ES', filter: { id: this.item['id'] } }
    ).then(resEsp => {
        this.item = resEsp['data'][0];
        this.api.send(this.component, 'get',
            { lang: 'EN', filter: { id: this.item['id'] } }
        ).then(res => {
            let itemEng = res['data'][0];
            let fields = this.formDef.map(register => register.filter(
                field => field['register_table'].indexOf('traduction') !== -1
            ).map(
                field => field['field_name'])
            ).filter(register => register.length);

            fields = fields.length ? fields[0] : [];

            if (itemEng) {
                this.item = Object.keys(itemEng).reduce((obj, key) => {
                    obj[key] = this.item[key];
                    if (fields.indexOf(key) !== -1) {
                        obj[key + '_eng'] = itemEng[key];
                    }
                    return obj;
                }, {});
            }

            if (this.item) {
                this.setForm();
            }
        })
    })
}

My specification file:我的规范文件:

it('should call api.send', () => {
    let spy1 = spyOn(api, 'send');
    let item = {
        id: 1,
        name: 'test',
    }

    component.addItem(item);
    component.itemToForm();

    expect(spy1).toHaveBeenCalled();
});

What is happening:怎么了:

The function itemToForm() is being called before the this.item is ready.this.item准备好之前调用函数itemToForm()

There are many strategies to avoid this error.有很多策略可以避免这个错误。 A very simple one is to add a catcher at the beginning of the function, like this:一个非常简单的方法是在函数的开头添加一个捕获器,如下所示:

itemToForm = () => {
  if(this.item === undefined) {return}
         
  // The rest of the code
}

This stops the function, if the data does not exist yet.如果数据尚不存在,这将停止函数。

A more elegant solution may be to go further up in your order of operations, and find who is calling itemToForm() and ensure the data exists prior to calling.一个更优雅的解决方案可能是在您的操作顺序中更进一步,并找到谁在调用itemToForm()并确保数据在调用之前存在。

I bumped to this question but my issue was actually something completely different.我碰到了这个问题,但我的问题实际上是完全不同的。

In my code for some reasons I had由于某些原因,在我的代码中

import { SOME_OBJECT } from '.';

which should instead be like this:而应该是这样的:

import { SOME_OBJECT } from './proper-file';

In case it's useful for anyone to know, I got the error Cannot read properties of undefined (reading 'spec') when attempting to install a local npm module with npm install [path to module here] .如果有人知道它对任何人都有用,我在尝试使用npm install [path to module here]安装本地 npm 模块时收到错误Cannot read properties of undefined (reading 'spec') The source of the problem: I hadn't given the local module a name in its package.json .问题的根源:我没有在其package.json中为本地模块命名。

在我的情况下,错误是相似的,但我得到了不同的解决方案,因为我在 JS 文件中使用 body-parser 模块,我必须添加

app.use(bodyParser.urlencoded({ extended: true }));

Typescript version error in package.json change typescript version to, Typescript版本错误package.json改typescript版本为,

"typescript": "3.8.3" “打字稿”:“3.8.3”

then npm install然后 npm 安装

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

相关问题 类型错误:无法读取未定义的属性(读取 &#39;<myvariable> &#39;) - TypeError: Cannot read properties of undefined (reading '<myvariable>') TypeError:无法读取未定义的属性(读取“getDeep”) - TypeError: Cannot read properties of undefined (reading 'getDeep') TypeError:无法读取未定义的属性(读取“管道”) - TypeError: Cannot read properties of undefined (reading 'pipe') TypeError:无法读取未定义的属性(读取“拆分”) - TypeError: cannot read properties of undefined (reading 'split') 类型错误:无法读取未定义的属性(读取“和”) - TypeError: Cannot read properties of undefined (reading 'and') TypeError:无法读取未定义的属性(正在读取“pageNumber”) - TypeError: Cannot read properties of undefined (reading 'pageNumber') 类型错误:无法读取未定义的属性(读取“fetchPersonnelList”) - TypeError: Cannot read properties of undefined (reading 'fetchPersonnelList') 类型错误:无法读取未定义的属性(读取“匹配”) - TypeError: Cannot read properties of undefined (reading 'match') 类型错误:无法读取未定义的属性(读取“拆分”)” - TypeError: Cannot read properties of undefined (reading 'split')" TypeError:无法读取未定义的属性(读取“baseURL”) - TypeError: Cannot read properties of undefined (reading 'baseURL')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM