首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:尝试创建对象数组时对常量变量的赋值

TypeError:尝试创建对象数组时对常量变量的赋值
EN

Stack Overflow用户
提问于 2021-02-15 21:02:29
回答 4查看 77关注 0票数 0

我一直在做一个刮板程序,在我尝试将抓取的数据对象推入数组之前,一切都很好。

现在我有这样的想法:

代码语言:javascript
复制
exports.parseData = (getLink, getDescription, getPrice, getPicture) => {
  const apartments = [];
  apartments = {
    link: getLink,
    descr: getDescription,
    price: getPrice,
    picture: getPicture,
  };
  console.log(apartments);
};

但是它抛出了一个错误:TypeError: Assignment to constant variable

现在,我认为这是由于数据发送到解析器的方式造成的。下面是我使用这段代码时数据的样子:

代码语言:javascript
复制
exports.parseData = (getLink, getDescription, getPrice, getPicture) => {
  console.log(getLink);
  console.log(getDescription);
  console.log(getPrice);
  console.log(getPicture);
  console.log('-----------------');
};
代码语言:javascript
复制
https://www.sant.ba/nekretnine/nekretnina-3467-dvosoban-stan-sa-liftom-u-samom-centru-grada-63-m2
 dvosoban stan sa liftom u samom centru grada , 63 m2 
199000.00
https://www.sant.ba/thumb.php?file=photos/3467/3467_1_1563271355.jpg&maxw=273&maxh=205
-----------------
https://www.sant.ba/nekretnine/nekretnina-3689-troiposoban-stan-u-naselju-sunca
 Troiposoban stan u naselju Sunca
360000.00
https://www.sant.ba/thumb.php?file=photos/3689/3689_1_1612344465.jpg&maxw=273&maxh=205
-----------------
https://www.sant.ba/nekretnine/nekretnina-3677-trosoban-renoviran-stan-u-naselju-dolac-malta-73-m2
 trosoban, renoviran stan u naselju Dolac malta, 73 m2
150000.00
https://www.sant.ba/thumb.php?file=photos/3677/3677_1_1608550332.jpg&maxw=273&maxh=205
-----------------
https://www.sant.ba/nekretnine/nekretnina-3537-cetverosoban-stan-u-novogradnji-sa-prelijepim-pogledom-na-grad-118-38-m2
 Četverosoban stan u novogradnji sa prelijepim pogledom na grad, 118,38 m2 
538090.37
https://www.sant.ba/thumb.php?file=photos/3537/3537_1_1573217155.jpg&maxw=273&maxh=205
-----------------
https://www.sant.ba/nekretnine/nekretnina-3536-trosoban-stan-u-novogradnji-sa-prelijepim-pogledom-na-grad-105-34-m2
 Trosoban stan u novogradnji sa prelijepim pogledom na grad, 105,34 m2
478817.70
https://www.sant.ba/thumb.php?file=photos/3536/3536_2_1573216638.jpg&maxw=273&maxh=205
-----------------
https://www.sant.ba/nekretnine/nekretnina-3535-luksuzni-cetverosoban-stan-na-cobaniji-117-43m2
 Luksuzni četverosoban stan na Čobaniji,117,43m2
533772.19
https://www.sant.ba/thumb.php?file=photos/3535/3535_13_1573215220.jpg&maxw=273&maxh=205
-----------------
https://www.sant.ba/nekretnine/nekretnina-3534-trosoban-stan-u-novogradnji-90-m2
 trosoban stan u novogradnji, 90 m2
376923.46
https://www.sant.ba/thumb.php?file=photos/3534/3534_8_1573214216.jpg&maxw=273&maxh=205
-----------------
etc....

现在我想要的输出是:拥有一个对象数组,其中对象填充数据,如上面的示例所示。因此,对于每个getLink, getDescription, getPrice, getPicture,创建新对象并将其推入数组(就像我在第一个代码块中使用它一样)。

我想要得到的输出是:

代码语言:javascript
复制
[
   {
   
      link: https://www.sant.ba/nekretnine/nekretnina-3467-dvosoban-stan-sa-liftom-u-samom-centru-grada-63-m2
      descr: dvosoban stan sa liftom u samom centru grada , 63 m2 
      price: 199000.00
       picture: https://www.sant.ba/thumb.php?file=photos/3467/3467_1_1563271355.jpg&maxw=273&maxh=205
   },
   {
     link:https://www.sant.ba/nekretnine/nekretnina-3689-troiposoban-stan-u-naselju-sunca
     descr: Troiposoban stan u naselju Sunca
     price: 360000.00
     picture: https://www.sant.ba/thumb.php?file=photos/3689/3689_1_1612344465.jpg&maxw=273&maxh=205
   },
   etc...
]

这样的事情很容易实现吗?谢谢!

以下是这些变量的发送位置:

代码语言:javascript
复制
const cheerio = require('cheerio');
const axios = require('axios');
const parsing = require('./parseData');

exports.olxScraper = () => {
  const url =
    'https://www.olx.ba/pretraga?vrsta=samoprodaja&kategorija=23&sort_order=desc&kanton=9&sacijenom=sacijenom&stranica=1';

  const getRawData = async () => {
    try {
      await axios.get(url).then((res) => {
        const $ = cheerio.load(res.data);
        $('div[id="rezultatipretrage"] > div')
          .not('div[class="listitem artikal obicniArtikal  i index"]')
          .not('div[class="obicniArtikal"]')
          .each((index, element) => {
            $('span[class="prekrizenacijena"]').remove();
            const getLink = $(element)
              .find('div[class="naslov"] > a')
              .attr('href');
            const getDescription = $(element)
              .find('div[class="naslov"] > a > p')
              .text();
            const getPrice = $(element)
              .find('div[class="datum"] > span')
              .text()
              .replace(/\.| ?KM$/g, '')
              .replace(' ', '');
            const getPicture = $(element)
              .find('div[class="slika"] > img')
              .attr('src');

            parsing.parseData(getLink, getDescription, getPrice, getPicture);
          });
      });
    } catch (error) {
      console.log(error);
    }
  };
  getRawData();
};

exports.santScraper = () => {
  const url = `https://www.sant.ba/pretraga/prodaja-1/tip-2/cijena_min-20000/stranica-1`;

  const getRawData = async () => {
    try {
      await axios.get(url).then((response) => {
        const $ = cheerio.load(response.data);

        $('div[class="col-xxs-12 col-xss-6 col-xs-6 col-sm-6 col-lg-4"]').each(
          (index, element) => {
            const getLink = $(element).find('a[class="re-image"]').attr('href');
            const getDescription = $(element).find('a[class="title"]').text();
            const getPrice = $(element)
              .find('div[class="prices"] > h3[class="price"]')
              .text()
              .replace(/\.| ?KM$/g, '')
              .replace(',', '.');
            const getPicture = $(element).find('img').attr('data-original');
            /*const getSquaremeters = $(element)
        .find('span[class="infoCount"]')
        .first()
        .text()
        .replace(',', '.')
        .split('m')[0];

      const pricepersquaremeter =
        parseFloat(getPrice) / parseFloat(getSquaremeters);
      articles[index] = {
        id: getLink.substring(42, 46),
        link: getLink,
        descr: getDescription,
        price: Math.round(getPrice),
        pictures: getPicture,
        sqm: Math.round(getSquaremeters),
        ppm2: Math.round(pricepersquaremeter),
      };*/
            parsing.parseData(getLink, getDescription, getPrice, getPicture);
          }
        );
      });
    } catch (error) {
      console.log(console.log(error));
    }
  };
  getRawData();
};

this.olxScraper();
this.santScraper();
EN

回答 4

Stack Overflow用户

发布于 2021-02-15 21:09:15

声明常量时,不能更改引用。在您的代码中,它表示为const apartments = [],因此您将const声明为数组,然后尝试将const声明为对象。

试一试

代码语言:javascript
复制
apartments.push({
    link: getLink,
    descr: getDescription,
    price: getPrice,
    picture: getPicture,
  });
票数 0
EN

Stack Overflow用户

发布于 2021-02-15 21:09:39

你的代码不仅仅有一个问题:

  1. 不能重新赋值const变量。

常量公寓= [];公寓={ ... };//这是不允许的,因为公寓是常量。

  1. 您将apartments定义为一个数组,然后尝试将一个对象赋给该变量。这在javascript中是允许的,但可能不是您想要的……

编辑

至于您的评论:如果您希望一个数组包含所有对象,则必须在函数外部定义数组,并将parseData函数定义为返回一个对象,然后可以将该对象推入该数组。

代码语言:javascript
复制
const apartments = [];
const getRawData = async () => {
    try {
      await axios.get(url).then((res) => {
        const $ = cheerio.load(res.data);
        $('div[id="rezultatipretrage"] > div')
          .not('div[class="listitem artikal obicniArtikal  i index"]')
          .not('div[class="obicniArtikal"]')
          .each((index, element) => {
            $('span[class="prekrizenacijena"]').remove();
            const getLink = $(element)
              .find('div[class="naslov"] > a')
              .attr('href');

            ....

            //push the result of parseData into the array
            apartments.push(parsing.parseData(getLink, getDescription, getPrice, getPicture));


           //if your parsedata just returns an object created from its parameters,
           //you can get rid of the function and just do 
           //apartments.push({
           //  link: getLink,
           //  descr: getDescription,
           //  price: getPrice,
           //  picture: getPicture           
           //});
          });
      });
    } catch (error) {
      console.log(error);
    }
  };

重新定义parseData,只返回一个对象。尽管看起来很不必要,但函数只返回其参数的一个对象。

代码语言:javascript
复制
exports.parseData = (getLink, getDescription, getPrice, getPicture) => {
  const apartment = {
    link: getLink,
    descr: getDescription,
    price: getPrice,
    picture: getPicture,
  };
  return apartment;
};
票数 0
EN

Stack Overflow用户

发布于 2021-02-15 21:10:19

你在和const一起申报公寓。

代码语言:javascript
复制
const apartments = [];

所以它被设置为一个数组。但随后您会尝试将其更改为一个对象。

代码语言:javascript
复制
apartments = {
    link: getLink,
    descr: getDescription,
    price: getPrice,
    picture: getPicture,
  };

无法更新或声明常量。一旦您将其声明为数组。它只能保留为数组。您可以推送到该数组,但不能将其更改为对象或任何其他数据类型。

在您的例子中,您可以直接将const公寓声明为对象。

代码语言:javascript
复制
const apartments = {
    link: getLink,
    descr: getDescription,
    price: getPrice,
    picture: getPicture,
  };

你可以在这里阅读更多关于var,let和const的内容:https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/

根据注释添加一个示例。

代码语言:javascript
复制
const data = [];

const parseData = (getLink, getDescription, getPrice, getPicture) => {
    const apartments = {
        link: getLink,
        descr: getDescription,
        price: getPrice,
        picture: getPicture,
      };
    data.push(apartments)
    console.log(data);
};

data的作用域不是parseData函数,因此可以包含推送到它的所有公寓对象。这个data数组应该在哪里声明,这在很大程度上取决于您的代码,并且可能也应该作为参数传递给parseData函数。

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

https://stackoverflow.com/questions/66208557

复制
相关文章

相似问题

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