繁体   English   中英

无法获取 javascript object 属性值

[英]can't get javascript object property value

我有这样的 javascript 代码

for (let x = 0; x < result.data.length; x++) {
  let tankMaterial = await readTank(result.data[x].tank_id);
  console.log(JSON.stringify(tankMaterial));

  parseObj[x] = {
    tank_id: result.data[x].tank_id,
    tank_name: result.data[x].tank_name,
    temperature: result.data[x].temperature,
    level_percent: Number.parseFloat(scale(result.data[x].pressure)).toFixed(2),
    level_raw: result.data[x].pressure,
    level_metric: (result.data[x].pressure * 6894.76) / (1000 * 9.8),
  };
}

console.log 的结果是:

{"id_material":"1","height":50,"diameter":25,"name":"CPO"}

我尝试从“tankMaterial”object 中提取“名称”属性值,代码如下:

for (let x = 0; x < result.data.length; x++) {
  let tankMaterial = await readTank(result.data[x].tank_id);
  console.log(JSON.stringify(tankMaterial.name));

  parseObj[x] = {
    tank_id: result.data[x].tank_id,
    tank_name: result.data[x].tank_name,
    temperature: result.data[x].temperature,
    level_percent: Number.parseFloat(scale(result.data[x].pressure)).toFixed(2),
    level_raw: result.data[x].pressure,
    level_metric: (result.data[x].pressure * 6894.76) / (1000 * 9.8),
  };
}

但它总是给出一个错误

 TypeError: Cannot read property 'name' of undefined
    at TankData (/var/www/html/demo1/api/TankData.js:20:43)

我一直在尝试从 tankMaterial object 访问另一个属性,但结果始终相同。

为什么我无法访问 tankMaterial object 的值?

您需要执行JSON.parse(tankMaterial).name才能访问它

let parseObj = [];
async function test(result) {
for (let index = 0; index < result.data.length; index ++) {
currentValue = result.data[index]
  // for (let currentValue of result.data) { can be used for good reading but 'continue' cannot be used
    let tankMaterial = await readTank(currentValue.tank_id).catch(() => {
      console.error('This promise failed')
      continue; // continue to next loop if some error in loop
    });
    console.log(tankMaterial) // this will be undefined if your promise has failed and you //if u don't use continue and start parsing/stringify it will throw you an error 

    try {
      console.log(tankMaterial.name)// if tankMaterial is undefined then u will get 'Cannot read property 'name' of undefined'
    } catch (error) {
      console.log(error) // log the error in case failure in your case name is not property of undefined 
    }

    console.log(tankMaterial);// {"id_material":"1","height":50,"diameter":25,"name":"CPO"}

    parseObj[index] = {
      tank_id: currentValue.tank_id,
      tank_name: currentValue.tank_name,
      temperature: currentValue.temperature,
      level_percent: Number.parseFloat(scale(currentValue.pressure)).toFixed(2),
      level_raw: currentValue.pressure,
      level_metric: (currentValue.pressure * 6894.76) / (1000 * 9.8),
    };
  }
}
  • 始终编写代码以使其可读
  • 捕获所有这些错误,以便您知道错误发生的确切位置
  • 我已经重构了这段代码,它现在应该很容易解决你的问题......快乐编码:)

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM