未处理的承诺拒绝
UnhandledPromiseRejection
大家好,我创建了一个网站,一切都在本地运行,但是当我在 heroku 中部署时,它不起作用,我找不到原因感谢您的帮助
heroku 错误:
2022-02-10T03:13:43.014545+00:00 app[web.1]: (node:22) 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(). 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). (rejection id: 1)
2022-02-10T03:13:43.014570+00:00 app[web.1]: (node:22) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
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:
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
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))
})
所有可以拒绝的await
的使用都必须以某种方式处理。它们必须被 try/catch
包围,否则 async
函数的调用者必须处理拒绝。
在这种情况下,app.get()
不会注意来自 async
回调的 returned/rejected 承诺,因此您必须在本地捕获拒绝。所以,在这段代码中:
//ecouter la methode GET et la route
app.get('/', async(req,res) => {
const indexHtml = await generatorModele('index')
res.send(indexHtml)
})
您没有收到 generatorModele()
的拒绝。您可以这样做:
//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);
}
});
所有,您对 await
的其他用途也需要类似的东西。
仅供参考,当您遇到此类问题并且错误消息没有准确指出问题所在时,首先要做的是确保您捕获并记录了所有可能的错误。我上面显示的这段代码就是这样一个例子。
大家好,我创建了一个网站,一切都在本地运行,但是当我在 heroku 中部署时,它不起作用,我找不到原因感谢您的帮助
heroku 错误:
2022-02-10T03:13:43.014545+00:00 app[web.1]: (node:22) 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(). 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). (rejection id: 1) 2022-02-10T03:13:43.014570+00:00 app[web.1]: (node:22) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. 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:
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
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))
})
所有可以拒绝的await
的使用都必须以某种方式处理。它们必须被 try/catch
包围,否则 async
函数的调用者必须处理拒绝。
在这种情况下,app.get()
不会注意来自 async
回调的 returned/rejected 承诺,因此您必须在本地捕获拒绝。所以,在这段代码中:
//ecouter la methode GET et la route
app.get('/', async(req,res) => {
const indexHtml = await generatorModele('index')
res.send(indexHtml)
})
您没有收到 generatorModele()
的拒绝。您可以这样做:
//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);
}
});
所有,您对 await
的其他用途也需要类似的东西。
仅供参考,当您遇到此类问题并且错误消息没有准确指出问题所在时,首先要做的是确保您捕获并记录了所有可能的错误。我上面显示的这段代码就是这样一个例子。