Home > front end >  UnhandledPromiseRejection
UnhandledPromiseRejection

Time:02-10

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 error:

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))
})

CodePudding user response:

All uses of await that can reject must be handled in some way. They must either be surrounded by a try/catch or the caller of the async function must handle the rejection.

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. 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(). 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.


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.

  •  Tags:  
  • Related