我正在嘗試列印/console.log 我從兩個單獨的 js 檔案中匯出的一些值。當我 console.log 上述值price并從它們各自的檔案中成功列印出來時,但是當我匯出這些值并info.secTableEN嘗試在 index.js 中將它們列印出來時,我得到的是. 我究竟做錯了什么?priceModulenameModule[object Object] [object Object]
索引.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());
uj5u.com熱心網友回復:
您在初始化之前需要并使用這些值,因此您實際上是在這樣做:
console.log({} {}); // [object Object][object Object]
相反,您應該匯出一個異步函式并在需要時呼叫它們(例如使用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());
然后,假設您對這兩個模塊都執行此操作,您可以將它們用于await:
const priceModule = require("./price");
const nameModule = require("./name");
(async () => {
console.log(await nameModule() await priceModule());
})();
uj5u.com熱心網友回復:
問題是您不能使用 將物件“添加”在一起 ,因此它們首先通過 toString() 強制轉換為字串,然后將這些字串連接起來。
因為toString()在一個物件上回傳"[object Object]"你得到"[object Object][object Object]".
請參閱下面代碼段中的示例/解決方案。
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
}
}
*/
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/528977.html
