简体   繁体   中英

I'm not getting the correct response on node

Im trying to send an Mail with node JS, and the terminal is showing me the next code: (node:13924) UnhandledPromiseRejectionWarning: TypeError: sgMail.send(...).then(...).cath is not a function

I'm not sure about why is happening it, all my development is into an API rest and the terminal only show me that error when I as for a request through the route.

This is my code:

const expressJwt = require('express-jwt')
const _ =require('lodash')
const { OAuth2Client } = require('google-auth-library')
const fetch = require('node-fetch')
const {validationResult} = require('express-validator')
const jwt = require('jsonwebtoken')
//Esto es para obtener el error de la  base de datos, se puede personalizar para hacerlo amigable.
const { errorHandler} =require('../helpers/dbErrorHandling')
//el siguiente const se usará para enviar correos
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.MAIL_KEY)

const pool = require('../database/connection')


class AuthController {
    async getAuth (req, res) {
        const result = await pool.query('select User_email from user');

        res.json({
            code: 200,
            message: "Prueba realizada con éxito",
            data: result
            
        });
    }

    async getAuths(req, res) {
    const email = (req.params.email);


    if(email.length == 0) {
        return res.json({
            code: 404,
            message: "Equipo no encontrado",
            data: [],
        })

    }else{
 

    //generate token

    const token = jwt.sign(
        {email}, 
         process.env.JWT_ACCOUNT_ACTIVATION,
        {expiresIn: '1440m' }
    )

    const emailData = {
      from: process.env.EMAIL_FROM,
      to: email,
      subject: 'Account activation link',
      html: `
                <h1>Please use the following to activate your account</h1>
                <p>${process.env.CLIENT_URL}/register/${token}</p>
                <hr />
                <p>This email may containe sensetive information</p>
                <p>${process.env.CLIENT_URL}</p>
            `
    }

    sgMail.send(emailData).then(sent => {
        return res.json({
            message: `el email se ha enviado a ${email}`
        })
    }).cath(err => {
        return res.status(404).json({
            error: errorHandler(err)
        })
    })

    }

        
    }

}


const authController = new AuthController();
module.exports = authController;

.catch(), misspelling errors get the best of us.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM