[英]UnhandledPromiseRejection
Hello guys i create a website and everything works locally but when i deployed in heroku its not working and i cant find why thanks for the help大家好,我创建了一个网站,一切都在本地工作,但是当我在 heroku 中部署时,它不工作,我找不到为什么感谢您的帮助
heroku error: heroku 错误:
2022-02-10T03:13:43.014545+00:00 app[web.1]: (node:22) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 2022-02-10T03:13:43.014545+00:00 app[web.1]: (node:22) UnhandledPromiseRejectionWarning: 未处理的 promise 拒绝。 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().此错误源于在没有 catch 块的情况下抛出异步 function,或者拒绝未使用 .catch() 处理的 promise。 To terminate the node process on unhandled promise rejection, use the CLI flag
--unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode ).要在未处理的 promise 拒绝时终止节点进程,请使用 CLI 标志--unhandled-rejections=strict
(请参阅https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode )。 (rejection id: 1) 2022-02-10T03:13:43.014570+00:00 app[web.1]: (node:22) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. (拒绝 ID:1)2022-02-10T03:13:43.014570+00:00 app[web.1]: (node:22) [DEP0018] DeprecationWarning: Unhandled promise 拒绝已弃用。 In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.以后,未处理的 promise 拒绝将以非零退出代码终止 Node.js 进程。 2022-02-10T03:14:13.009344+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/" host=websitechina.herokuapp.com request_id=ab93f3f6-9ccf-49e0-be7b-fe765239c47b fwd="61.219.114.7" dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 protocol=https 2022-02-10T03:14:13.009344+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/" host=websitechina.herokuapp.com request_id=ab93f3f6-9ccf -49e0-be7b-fe765239c47b fwd="61.219.114.7" dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 protocol=https
index.js:索引.js:
const express = require('express') const app = express() const port = process.env.port || 3000 const nodemailer = require('nodemailer') //Importer la logique de la page d acceuil const generatorModele = require('./api/page/page-get') //ecouter la methode GET et la route app.get('/', async(req,res) => { const indexHtml = await generatorModele('index') res.send(indexHtml) }) app.get ('/tech', async(req,res) => { const techHtml = await generatorModele('tech') res.send(techHtml) }) app.get ('/contact', async(req,res) => { const contactHtml = await generatorModele('contact') res.send(contactHtml) }) app.get ('/signUp', async(req,res) => { const signUpHtml = await generatorModele('signUp') res.send(signUpHtml) }) //ecouter la methode Post et la route app.post('/', (req,res)=>{ console.log(req.body) //nodemailer not working yet i know // const transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: '', pass: '' } }) transporter.sendMail(mailOptions, (error, info)=>{ if(error){ console.log(error) res.send('error') }else{ console.log('Email sent' + info.response) res.send('success') } }) }) //ecoute les requete du repertoire styles,js,images/ app.use('/styles', express.static('/Users/hi/code/dotfiles/dev/websitechina1/styles')) app.use('/images', express.static('/Users/hi/code/dotfiles/dev/websitechina1/images/')) app.use('/js', express.static('/Users/hi/code/dotfiles/dev/websitechina1/js/')) app.use(express.json()) //demarrer le serveur et ecouter un port donne app.listen(process.env.PORT || port,() => { console.log (`Example app listening on port ${port}`) })
app.js应用程序.js
const menu = document.querySelector('#mobile-menu'); const menuLinks = document.querySelector('.navbar_menu'); menu.addEventListener('click', function(){ menu.classList.toggle('is-active'); menuLinks.classList.toggle('active'); }); const contactForm = document.querySelector('.contact-form') let fname = document.getElementById('fname') let lname = document.getElementById('lname') let email = document.getElementById('email') let country = document.getElementById('country') let subject = document.getElementById('subject') contactForm.addEventListener('submit', (e)=>{ e.preventDefault(); let formData = { fname: fname.value, lname: lname.value, email: email.value, country: country.value, subject: subject.value } let xhr = new XMLHttpRequest(); xhr.open('POST', '/') xhr.setRequestHeader('content-type','application/json') xhr.onload = function(){ console.log(xhr.responseText) if (xhr.responseText == 'success'){ alert('Email sent'); fname.value = ''; lname.value = ''; email.value = ''; country.value = ''; subject.value = ''; }else{ alert('Something went wrong') } } xhr.send(JSON.stringify(formData)) })
All uses of await
that can reject must be handled in some way.必须以某种方式处理所有可以拒绝的await
用法。 They must either be surrounded by a try/catch
or the caller of the async
function must handle the rejection.它们必须被try/catch
包围,否则async
function 的调用者必须处理拒绝。
In this case, app.get()
does not pay any attention to a returned/rejected promise from the async
callback so you have to catch rejections locally.在这种情况下, app.get()
不会关注async
回调中返回/拒绝的 promise,因此您必须在本地捕获拒绝。 So, in this code:所以,在这段代码中:
//ecouter la methode GET et la route
app.get('/', async(req,res) => {
const indexHtml = await generatorModele('index')
res.send(indexHtml)
})
You aren't catching a rejection from generatorModele()
.您没有收到generatorModele()
的拒绝。 You can do that like this:你可以这样做:
//ecouter la methode GET et la route
app.get('/', async(req,res) => {
try {
const indexHtml = await generatorModele('index');
res.send(indexHtml);
} catch(e) {
console.log(e);
res.sendStatus(500);
}
});
All, your other uses of await
need something similar.所有,您对await
的其他用途都需要类似的东西。
FYI, when you have a problem like this and the error message doesn't point exactly to the problem, the first thing to do is make sure you're catching and logging all possible errors.仅供参考,当您遇到这样的问题并且错误消息没有准确指出问题时,首先要做的是确保您正在捕获并记录所有可能的错误。 This code I show above is one such example.我上面显示的这段代码就是这样一个例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.