简体   繁体   English

Visual Studio 在 JavaScript 中显示错误“无法访问的代码”

[英]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:我正在使用 NODE 的 API 工作,但在编写 VC 时,在下一个代码中显示错误“无法访问的代码”:

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.在您的情况下,您从 function return ifelse分支。 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) if (email.length = 0) fails at runtime because length is a read-only property of a string. if (email.length = 0)在运行时失败,因为length是字符串的只读属性。 You cannot put a 0 value into it.您不能将0值放入其中。

There is a Spanish-language pack for Visual Studio Code here .这里有一个适用于 Visual Studio Code 的西班牙语包 You may find it helpful, especially if you are just starting to learn.您可能会发现它很有帮助,尤其是当您刚刚开始学习时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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