我正在尝试测试一些功能。在routes.js文件中,我放置了这段代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
<tr>
<td>
<img SRC="IMG/blfy.gif" ALT="Butterflies" id="img1" class='img' />
</td>
<td>
<img SRC="IMG/eye.gif" ALT="Eye" id="img2" class='img' />
</td>
<td>
<img SRC="IMG/wave.gif" ALT="Wave" id="img3" class='img' />
</td>
<td>
<IMG SRC="IMG/jungle.gif" ALT="Jungle" id="img4" class='img' />
</td>
</tr>
<tr>
<td>
<IMG SRC="IMG/bridge.gif" ALT="Bridge" id="img5" class='img' />
</td>
<td>
<IMG SRC="IMG/duck.gif" ALT="Duck" id="img6" class='img' />
</td>
<td>
<IMG SRC="IMG/egg.gif" ALT="Eggs" id="img7" class='img' />
</td>
<td>
<IMG SRC="IMG/aurora.gif" ALT="Aurora" id="img8" class='img' />
</td>
</tr>
<tr>
<td>
<IMG SRC="IMG/it.gif" ALT="Technology" id="img9" class='img' />
</td>
<td>
<IMG SRC="IMG/hill.gif" ALT="Hills" id="img10" class='img' />
</td>
<td>
<IMG SRC="IMG/string.gif" ALT="strings" id="img11" class='img' />
</td>
<td>
<IMG SRC="IMG/vegi.gif" ALT="vegetables" id="img12" class='img' />
</td>
</tr>
</table>
<br>
<button class="botton" id="dispbtn">Display</button>
<button class="botton" id="zoombtn">Zoom</button>
</body>
</html>
我读过async / await(这是我第一次使用它)。虽然我收到错误消息,但代码不起作用:
async function getPic(arg) {
const browser = await puppeteer.launch(/*{headless: false}*/);
const page = await browser.newPage();
await page.goto(arg);
await page.setViewport({width: 1000, height: 500})
await page.screenshot({path: 'pic.png'});
await broswer.close();
}
不幸的是我不知道这意味着什么。我在stackoverflow上发现了一个类似的问题: NodeJS Unhandled Promise Rejection
但遗憾的是,对于我如何解决此错误并不清楚。
当我在一个独立的节点环境中测试这段代码时 - 意味着这个代码没有任何其他东西,就像我在我的项目中那样,然后以某种方式运行。
当我将此函数放在我的routes.js文件中,然后在发生事件时调用该函数,然后我得到错误。
以下是调用此函数的代码:
(node:5896) UnhandledPromiseRejectionWarning: Error: Navigation Timeout Exceeded
: 30000ms exceeded
at Promise.then (C:\...\node_modules\puppeteer\lib\NavigatorWatcher.js:73:21
)
at <anonymous>
(node:5896) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejection
id: 1)
(node:5896) [DEP0018] DeprecationWarning: Unhandled promise rejections are depre
cated. In the future, promise rejections that are not handled will terminate the
Node.js process with a non-zero exit code.
答案 0 :(得分:1)
您需要await getPic(url);
。
此外,您在await broswer.close();
答案 1 :(得分:1)
你应该在调用异步函数之前使用await,并且将try调用等待调用try catch构造。
app.post('/sc', async (req, res) => {
const url = req.body.convo
try {
var picture = await getPic(url);
//some logic or render response
} catch (error){
//here you should handle error
}
})