Home > Net >  Data from module.exports prints out [object Object]
Data from module.exports prints out [object Object]

Time:11-08

I'm trying to print/console.log some values I'm exporting from two separate js files. When I console.log the said values price and info.secTableEN from their individual files it prints out successfully, but when I export thses values and try printing them out in index.js as priceModule and nameModule respectfully what I get is [object Object] [object Object]. What am I doing wrong?

Index.js

const priceModule = require("./price");
const nameModule = require("./name");

console.log(nameModule   priceModule);

nameModule.js

const puppeteer = require("puppeteer");

// Url where we get and scrape the data from
const url = "https://www.sec.gov/edgar/search/#/dateRange=custom&category=custom&startdt=2017-11-05&enddt=2022-11-07&forms=4";

let browser;
(async () => {
  browser = await puppeteer.launch();
  const [page] = await browser.pages();
  const $ = (...args) => page.waitForSelector(...args);
  const text = async (...args) =>
    (await $(...args)).evaluate(el => el.textContent.trim());
  await page.goto(url, {waitUntil: "domcontentloaded"});
  const info = {
    secTableEN: await text(".table td.entity-name"),
    secTableFiled: await text(".table td.filed"),
    secTableLink: await text(".table td.filetype"),
  };

  module.exports = info.secTableEN;
})()
  .catch(err => console.error(err))
  .finally(() => browser?.close());

priceModule.js

const puppeteer = require("puppeteer");

// Url where we get and scrape the data from
const url = "https://www.sec.gov/edgar/search/#/dateRange=custom&category=custom&startdt=2017-11-05&enddt=2022-11-07&forms=4";

(async () => {
    browser = await puppeteer.launch();
    const [page] = await browser.pages();
    const ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36";
    await page.setUserAgent(ua);
    await page.goto(url, {waitUntil: "domcontentloaded", timeout: 0});
    const responseP = page.waitForResponse(res =>
      res.status() === 200 && res.url().endsWith(".xml")
    );
    const a = await page.waitForSelector(".filetype .preview-file");
    await a.click();
    const html = await (await responseP).text();
    await page.evaluate(html => document.body.outerHTML = html, html);
    const price = await page.$$eval(".FormText", els =>
      els.find(e => e.textContent.trim() === "$")
        .parentNode
        .textContent
        .trim()
    );

    module.exports = price;

  })()
    .catch(err => console.error(err))
    .finally(() => browser?.close());

CodePudding user response:

You're requiring and using the values before they are initialized, so you're actually doing this:

console.log({}   {}); // [object Object][object Object]

Instead, you should export an async function and call those when you require them (example using nameModule.js):

// export function
module.exports = () => (async () => {
  browser = await puppeteer.launch();
  const [page] = await browser.pages();
  const $ = (...args) => page.waitForSelector(...args);
  const text = async (...args) =>
    (await $(...args)).evaluate(el => el.textContent.trim());
  await page.goto(url, {waitUntil: "domcontentloaded"});
  const info = {
    secTableEN: await text(".table td.entity-name"),
    secTableFiled: await text(".table td.filed"),
    secTableLink: await text(".table td.filetype"),
  };

  return info.secTableEN;
})()
  .catch(err => console.error(err))
  .finally(() => browser?.close());

Then, assuming you did this with both modules, you would use them with await:

const priceModule = require("./price");
const nameModule = require("./name");

(async () => {
     console.log(await nameModule()   await priceModule());
})();

CodePudding user response:

The issue is that you can't "add" objects together using , so they get coerced to strings via toString() first, then those strings get concatenated.

Because toString() on an object returns "[object Object]" you get "[object Object][object Object]".

See examples/solutions in the snippet below.

const foo = { someProp: 123 };
const bar = { otherProp: 456 };

// console knows how to handle objects
console.log(foo); // { "someProp": 123 }
console.log(bar); // { "otherProp": 456 }

// toString() produces [object Object]
console.log(foo.toString()); // [object Object]

// attempting to concatenate causes coersion toString() on both objects
console.log( foo   bar ); // [object Object][object Object]

// solutions
console.log( foo, bar ); // { "someProp": 123 } {"otherProp": 456 }
console.log({ foo, bar });
/*
{
  "foo": {
    "someProp": 123
  },
  "bar": {
    "otherProp": 456
  }
}
*/

  • Related