类属性无法读取,因为它未定义。

huangapple go评论61阅读模式
英文:

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 &#39;image&#39;)

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 &lt; rootNodes.length; i++) {
    image = rootNodes[i].querySelector(&quot;.product-img&quot;).getAttribute(&quot;src&quot;);
    volume = rootNodes[i].querySelector(&quot;.co-product__volume&quot;).textContent;
    name = rootNodes[i].querySelector(&quot;.co-product__anchor&quot;).textContent;
    url = rootNodes[i].querySelector(&quot;.co-product__anchor&quot;).href;
    price = rootNodes[i].querySelector(&quot;.co-product__price&quot;).childNodes[1];
    uom = rootNodes[i].querySelector(&quot;.co-product__price-per-uom&quot;).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: &quot;image&quot;,
  volume: &quot;volume&quot;,
  name: &#39;name&#39;,
  url: &#39;url&#39;,
  price: &#39;price&#39;,
  uom: &#39;uom&#39;,
}
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;
        }
    }

huangapple
  • 本文由 发表于 2023年6月15日 09:18:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定