【发布时间】:2020-10-02 01:52:28
【问题描述】:
背景知识:我正在构建一个工具,可以按计划截取网页截图。它还存储了有关如何截取屏幕截图的设置(例如,隐藏某些页面元素,等待 X 秒以防长轮询请求或具有动画的启动页面)
很自然,我必须从数据库中读取这些内容,然后循环这些结果以运行基于这些变量的代码块。
我可以很好地输出我的记录。我可以单独遍历它们就好了。但是当我尝试插入并播放现有的代码块时,事情就崩溃了。不知何故,这打破了我的循环,页面下方的代码无法识别我的数据库循环中的变量。
假设我的数据库记录被分配给变量poo,并且在此之前我对自己的语法有信心:
for (var i = 0; i < poo.length; i++) {
console.log('The name of this task is ' + poo[i].PrettyName);
//So far so good! I can output my column names as variables and do what I want with them.
Screenshot(poo[i].URL);
//This works too!
async function Screenshot(url) {
const browser = await puppeteer.launch({
headless: true,
defaultViewport: {width: 1920, height: 1080},
args: [
"--no-sandbox",
"--disable-gpu",
]
});
//This is not the end of my code, but basically nothing after this point works.
//When I try to read or log any variable,
//even ones that worked before, it tells me "cannot read property ___ of undefined."
我是否不小心在某处用括号破坏了循环?这是异步函数魔法吗?当我必须应用回调时有什么误解?
即使我在异步函数运行之前手动输入了 10 秒的延迟,变量仍然会中断,这应该足以形成一个记录集进行处理。
你看到的代码的肉和土豆是puppeteer模块。
【问题讨论】:
标签: javascript node.js puppeteer node-modules