为什么对象属性变得未定义?

问题描述 投票:0回答:1

在创建航班的用例中,我需要获取用户提供 id 的飞机的总容量。因此,我从指定 id 的表中检索飞机对象。 其代码如下:

获取飞机的Repository层代码:

async get(id){
        console.log(id);
        try{
            const modelData=await this.model.find({
                where:{id}
            })
            console.log(modelData);
            return modelData;
        }
        catch(error){
            console.log("Something went wrong in the repository layer "+ error);
        }
    }

服务层航班创建代码:

async create(data){
       
        try{
            const airplane = await this.airplaneRepo.get(data.airplaneId);
            console.log(airplane);
            const flight =await this.crudRepo.create(data);
            return flight;
        }
        catch(error){
            console.log("Something went wrong in the service layer " + error );
        }
    }

因此,当我按照上面的代码记录飞机时,我得到以下对象:

[
  Airplane {
    dataValues: {
      id: 1,
      modelNumber: 'Boeing 737',
      capacity: 250,
      createdAt: 2023-12-19T12:07:24.000Z,
      updatedAt: 2023-12-19T12:07:24.000Z
    },
    _previousDataValues: {
      id: 1,
      modelNumber: 'Boeing 737',
      capacity: 250,
      createdAt: 2023-12-19T12:07:24.000Z,
      updatedAt: 2023-12-19T12:07:24.000Z
    },
    uniqno: 1,
    _changed: Set(0) {},
    _options: {
      isNewRecord: false,
      _schema: null,
      _schemaDelimiter: '',
      raw: true,
      attributes: [Array]
    },
    isNewRecord: false
  }
]

当我尝试访问这架飞机的容量属性时,它给了我未定义的信息。

javascript model sequelize.js undefined sequelize-cli
1个回答
-2
投票

对象属性可能因各种原因而变得未定义。常见原因包括访问不存在的 property、引用未定义的变量或在属性赋值期间遇到错误。要进行故障排除,请检查代码是否有拼写错误,验证属性是否存在,并处理属性访问或分配期间的潜在错误。

© www.soinside.com 2019 - 2024. All rights reserved.