[英]can't get javascript object property value
I have javascript code like this我有这样的 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),
};
}
the result from the console.log is: console.log 的结果是:
{"id_material":"1","height":50,"diameter":25,"name":"CPO"}
I try to extract "name" property value from the "tankMaterial" object with code like:我尝试从“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),
};
}
but it always gives an error但它总是给出一个错误
TypeError: Cannot read property 'name' of undefined
at TankData (/var/www/html/demo1/api/TankData.js:20:43)
I've been trying to access another property from tankMaterial object but the result always the same.我一直在尝试从 tankMaterial object 访问另一个属性,但结果始终相同。
why can't I access the value of the tankMaterial object?为什么我无法访问 tankMaterial object 的值?
You need to do JSON.parse(tankMaterial).name
to access it您需要执行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.