英文:
Class property can't be read because its undefined
问题
I'm trying to create a new object. I'm passing a dictionary to the constructor, but I keep getting an Uncaught TypeError: Cannot read properties of undefined (reading 'image')
class Product {
constructor(data) {
this.image = this.data.image;
this.volume = this.data.volume;
this.name = this.data.name;
this.url = this.data.url;
this.price = this.data.price;
this.uom = this.data.uom;
}
}
for (let i = 0; i < rootNodes.length; i++) {
image = rootNodes[i].querySelector(".product-img").getAttribute("src");
volume = rootNodes[i].querySelector(".co-product__volume").textContent;
name = rootNodes[i].querySelector(".co-product__anchor").textContent;
url = rootNodes[i].querySelector(".co-product__anchor").href;
price = rootNodes[i].querySelector(".co-product__price").childNodes[1];
uom = rootNodes[i].querySelector(".co-product__price-per-uom").textContent;
var product = new Product({
image: image,
volume: volume,
name: name,
url: url,
price: price,
uom: uom
});
products.push(product);
}
I can't see what I'm doing wrong here, can someone please explain? Thank you.
I haven't tried anything, yet.
英文:
I'm trying to create a new object. I'm passing a dictionary to the constructor, but I keep getting an Uncaught TypeError: Cannot read properties of undefined (reading 'image')
class Product {
constructor(data) {
this.image = this.data.image;
this.volume = this.data.volume;
this.name = this.data.name;
this.url = this.data.url;
this.price = this.data.price;
this.uom = this.data.uom;
}
}
for (let i = 0; i < rootNodes.length; i++) {
image = rootNodes[i].querySelector(".product-img").getAttribute("src");
volume = rootNodes[i].querySelector(".co-product__volume").textContent;
name = rootNodes[i].querySelector(".co-product__anchor").textContent;
url = rootNodes[i].querySelector(".co-product__anchor").href;
price = rootNodes[i].querySelector(".co-product__price").childNodes[1];
uom = rootNodes[i].querySelector(".co-product__price-per-uom").textContent;
var product = new Product({
image: image,
volume: volume,
name: name,
url: url,
price: price,
uom: uom
});
products.push(product);
}
I can't see what I'm doing wrong here, can someone please explain? Thank you.
I haven't tried anything, yet.
答案1
得分: 2
你正在尝试从this.data
获取数据,但它将是undefined
,所以你必须使用:
constructor(data) {
this.image = data.image;
this.volume = data.volume;
this.name = data.name;
this.url = data.url;
this.price = data.price;
this.uom = data.uom;
}
举例来说,你可以看到如下代码:
class Product {
constructor(data) {
this.image = data.image;
this.volume = data.volume;
this.name = data.name;
this.url = data.url;
this.price = data.price;
this.uom = data.uom;
}
}
let newObj = {
image: "image",
volume: "volume",
name: 'name',
url: 'url',
price: 'price',
uom: 'uom',
}
let product = new Product(newObj);
console.log(product);
英文:
You are trying to get data from this.data
which will be undefined
so you have to use
constructor(data) {
this.image = data.image;
this.volume = data.volume;
this.name = data.name;
this.url = data.url;
this.price = data.price;
this.uom = data.uom;
}
For the sake of exmple you can see as:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
class Product {
constructor(data) {
this.image = data.image;
this.volume = data.volume;
this.name = data.name;
this.url = data.url;
this.price = data.price;
this.uom = data.uom;
}
}
let newObj = {
image: "image",
volume: "volume",
name: 'name',
url: 'url',
price: 'price',
uom: 'uom',
}
let product = new Product(newObj);
console.log(product);
<!-- end snippet -->
答案2
得分: 0
你的代码问题在于你在Product构造函数内部访问数据对象的属性方式。你试图从未定义的变量this.data中访问属性,而应该直接使用data而不带有this关键字来修复这个问题。以下是已修正的代码:
class Product {
constructor(data) {
this.image = data.image;
this.volume = data.volume;
this.name = data.name;
this.url = data.url;
this.price = data.price;
this.uom = data.uom;
}
}
英文:
The issue in your code lies in how you are accessing the properties of the data object within the Product constructor. Instead of accessing the properties directly, you are trying to access them from an undefined variable this.data. To fix this, you should use data directly without the this keyword. Here's the corrected code:
class Product {
constructor(data) {
this.image = data.image;
this.volume = data.volume;
this.name = data.name;
this.url = data.url;
this.price = data.price;
this.uom = data.uom;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论