首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法获得javascript对象属性值

问无法获得javascript对象属性值
EN

Stack Overflow用户
提问于 2021-04-28 11:51:18
回答 2查看 74关注 0票数 0

我有这样的javascript代码

代码语言: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的结果是:

代码语言:javascript
复制
{"id_material":"1","height":50,"diameter":25,"name":"CPO"}

我尝试从"tankMaterial“对象中提取"name”属性值,代码如下:

代码语言:javascript
复制
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),
  };
}

但它总是会犯错误

代码语言:javascript
复制
 TypeError: Cannot read property 'name' of undefined
    at TankData (/var/www/html/demo1/api/TankData.js:20:43)

我一直试图从tankMaterial对象访问另一个属性,但是结果总是一样的。

为什么我不能访问tankMaterial对象的值?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-28 14:47:14

代码语言:javascript
复制
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),
    };
  }
}

  • 总是编写您的代码,以便它是可读的,
  • 捕获所有这些错误,以便您知道错误发生在哪里
  • ,我重构了这段代码,它现在应该很容易修复您的问题…快乐编码:)
票数 0
EN

Stack Overflow用户

发布于 2021-04-28 11:54:36

您需要执行JSON.parse(tankMaterial).name来访问它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67299702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档