TypeError: Assignment to constant variable, when trying to create an array of objects

I’ve been working on a scraper and all was good until I’ve tried to push objects of scraped data into an array.

Right now I have this:

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

But it throws an error: TypeError: Assignment to constant variable.

Now I think this is happening because of how data is sent to a parser.
Here is how data looks when I use this code:

exports.parseData = (getLink, getDescription, getPrice, getPicture) => {
  console.log(getLink);
  console.log(getDescription);
  console.log(getPrice);
  console.log(getPicture);
  console.log('-----------------');
};


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....

Now what my desired output would be: To have an array of objects, where objects are filled with data like it’s shown in example above. So for each getLink, getDescription, getPrice, getPicture create new object and push it into an array(like I’m using it in the first code block).

Output that I would like to get is:

[
   {
   
      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...
]

Is something like this easy to achieve?
Thanks!

Here is from where these variables are sent:

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();

64 thoughts on “TypeError: Assignment to constant variable, when trying to create an array of objects”

  1. pharmacie de garde ouverte aujourd’hui hyeres pharmacie ouverte xertigny , pharmacie x rousse pharmacie bourges nord , Barato Acivir en Ecuador, DГіnde comprar Acivir 200 mg. Comprar Acivir sin receta Comprar Acivir genГ©rico [url=https://kit.co/lucosri/acivir-en-farmacia-ecuador-barato-acivir-en-ecuador#]Comprar Acivir genГ©rico[/url] pharmacie ouverte ile d’oleron Comprar Lexapro medicamento, DГіnde comprar Lexapro 10 mg. Comprar Lexapro sin receta Comprar Lexapro genГ©rico [url=https://kit.co/veculci/comprar-lexapro-medicamento-lexapro-en-farmacia-miami#]Comprar Lexapro genГ©rico[/url] , therapies systemiques familiales pharmacie leclerc erstein , therapie comportementale et cognitive toulouse. pharmacie leclerc nord pharmacie annecy albigny DГіnde comprar Bupropion 150 mg, Comprar Bupropion medicamento. Comprar Bupropion sin receta Comprar Bupropion genГ©rico [url=https://kit.co/ofpruper/donde-comprar-bupropion-150-mg-barato-bupropion-en-espana#]DГіnde comprar Bupropion 150 mg[/url] , medicaments utilises en soins palliatifs. pharmacie de garde marseille 6, pharmacie auchan gien pharmacie guillot avignon . pharmacie amiens ouvert dimanche, pharmacie houdan therapie genique cancer pharmacie kremlin bicetre .

    Reply
  2. therapie de couple bordeaux pharmacie beauvais st lucien , pharmacie colmar pharmacie bordeaux ginko , Tadapox en farmacia Chile, DГіnde comprar Tadapox 80 mg. Comprar Tadapox sin receta Comprar Tadapox genГ©rico [url=https://kit.co/neumohun/tadapox-en-farmacia-chile-comprar-tadapox-sin-receta#]Comprar Tadapox sin receta[/url] pharmacie ouverte tours Comprar Tricor genГ©rico, DГіnde comprar Tricor 160 mg. Comprar Tricor sin receta Comprar Tricor genГ©rico [url=https://kit.co/quedinigh/donde-comprar-tricor-160-mg-comprar-tricor-generico#]Tricor en farmacia Guatemala[/url] , act therapy for depression pharmacie ouverte nice , pharmacie auchan buchelay. pharmacie basilique argenteuil therapies alternatives geneve Comprar Tadora medicamento, Comprar Tadora medicamento. Tadora en farmacia Argentina Comprar Tadora genГ©rico [url=https://kit.co/riorosuc/comprar-tadora-medicamento-tadora-en-farmacia-argentina#]Comprar Tadora genГ©rico[/url] , therapies act. therapies de conversion arte, pharmacie aix en provence jas de bouffan traitement gingivite . therapies cognitivo-comportementales exemple, raw therapee salon des therapies alternatives lorient pharmacie angers rue paul bert .

    Reply
  3. Pingback: sildenafil
  4. Pingback: viagra buy online
  5. Pingback: viagra for sale
  6. Pingback: cialis 20
  7. Pingback: free viagra
  8. Pingback: herbal viagra

Leave a Comment