因此,我有一个表单,其中包含代表教育信息的两组相同的输入。可能有两个以上,因为我想添加一个按钮来创建一个新组,以便用户可以像在 LinkedIn 中一样输入他所有的教育背景。
\n\n<form id="formCV" action="">\n <div id="educationContainer">\n <!-- First Group -->\n <div class="education">\n <div>\n <input type="text" name="institutionName">\n </div>\n <div>\n <input type="text" name="courseName">\n </div>\n <div>\n <input type="month" name="startDate">\n </div>\n <div>\n <input type="month" name="endDate">\n </div>\n </div>\n <!-- Second Group -->\n <div class="education">\n <div>\n <input type="text" name="institutionName">\n </div>\n <div>\n <input type="text" name="courseName">\n </div>\n <div>\n <input type="month" name="startDate">\n </div>\n <div>\n <input type="month" name="endDate">\n </div>\n </div>\n </div>\n</form>\nRun Code Online (Sandbox Code Playgroud)\n\n现在,如果我使用 FormData API 来获取表单数据,如下所示:
\n\nfor(let entry of formData.entries()){\n console.log(entry);\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我得到以下输出:
\n\n(2)\xc2\xa0["institutionName", "Harvard"]\n(2)\xc2\xa0["courseName", …Run Code Online (Sandbox Code Playgroud) 特定事件发生后,如何继续使用我的 puppeteer 代码?以一种同步的方式。例如,我有以下“请求”事件代码:
await page.on('request', request => {
if (request.resourceType() === 'xhr'){
//do something
}
});
Run Code Online (Sandbox Code Playgroud)
我的 puppeteer 代码依赖于特定页面上的 AJAX 调用响应来填写表单。因此,我想我必须以某种方式创建请求事件函数,以便在满足条件时解析承诺,以便在代码中稍后将其用作参考,但我希望获得有关该主题的一些指导,因为我不太熟悉使用异步代码,也不使用 Promise 或 puppeteer。谢谢!
所以我在 nodeJS 中使用 puppeteer 并且我有一个递归调用自身的函数,直到正确加载指定的页面:
const isPageAccessible = async (page, url) => {
var pageRes = await page.goto(url, {
timeout: 10000
}).catch(e => console.log(e));
if (!pageRes || pageRes.status !== 200) {
console.log("Website didn't load properly.")
isPageAccessible(page, url);
} else {
console.log("Loaded!")
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是这个函数在第一次递归调用后返回 undefined (据我所知这是正常的,因为异步函数必须用一个值解析)。我希望代码等到这个函数解决true
console.log(await isPageAccessible(page,LOGIN_URL));
console.log("Done!")
Run Code Online (Sandbox Code Playgroud)
所以控制台会记录“完成!” 网站加载成功后。现在它正在记录“完成!” 即使网站尚未加载,因为isPageAccessible函数在第一次调用后返回 undefined。
任何有关如何解决此问题的想法将不胜感激!
我有这个界面:
interface Field {
type: string;
value: (argument1: string, argument2: string) => string;
}
const field: Field = {
type: 'text',
value: () => 'Hello'
};
Run Code Online (Sandbox Code Playgroud)
为什么 typescript 不强制执行argument1and argument2?这段代码编译没有问题,我想要打字稿来确保该函数与我在接口定义中键入的完全相同。