简体   繁体   中英

Visual Studio shows error "Unreachable code" in JavaScript

I'm working in an API with NODE, but at the moment of are writing VC shows error "Unreachable code", on the next 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')

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








exports.AuthController = (req, res) => { 
    const email = (req.params.email);
    let sql = `select User_email from user where User_email = ${email}`;
    const mail = poo.query(sql);

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

    }else{
        return res.json({
            code: 404,
            message: "",
        })

    //generate token

    *const token = jwt.sign({
        email
    }, 
    process.env.ACCES_TOKEN_SECRET,{
        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}/users/activate/${token}</p>
                <hr />
                <p>This email may containe sensetive information</p>
                <p>${process.env.CLIENT_URL}</p>
            `
    }

    }

}*

The content that is not receiving me is whats is between " * ", so if know what it's happened I'll be grateful for an explanation.

Do you mean unreachable code where you mention unresearch code ? That message means the code can never run.

In your case you return from your function on both the if and else branch. The compiler is pretty smart about this.

if(email.length = 0) {
    return res.json({                    /* returns, exiting function */
        code: 404,
        message: "Equipo no encontrado",
        data: [],
    })
} else {
    return res.json({                    /* also returns, exiting function */
        code: 404,
        message: "",
    })
}
/* execution can never get here, so code below is unreachable. */

You want if (email.length === 0) . if (email.length = 0) fails at runtime because length is a read-only property of a string. You cannot put a 0 value into it.

There is a Spanish-language pack for Visual Studio Code here . You may find it helpful, especially if you are just starting to learn.

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