代码之家  ›  专栏  ›  技术社区  ›  yavg

字符串变得不可变,我甚至不能从中删除空格或修改它

  •  -2
  • yavg  · 技术社区  · 5 年前

    朋友们,我知道这个标题说的不多,但我有这个拦截器好几天了。

    在下面的代码中,一切都可以工作,但在我的真实代码中却不行,我无法上传所有代码来重现错误。

    我正在实施 middleware 在里面 nodejs 它验证用户的角色,当它在最后进行验证时,它永远不会进入我的 if 因为情况总是负面的。

    无论我做什么,我都无法做到 mutate 价值 req.user.rol (我的原始代码就是这样,我无法上传原始值,因为如果我 JSON.stringify() 生成循环结构并生成错误)

     //let role = req.user.rol; my original code
     let role = data.rol; // code of this example
     console.log(role) -> 'Administrator General'
    

    我试过服用 spaces 在该值之外,还尝试将其转换为 json ,然后是字符串,再返回 json 试图 变异 它的价值,但它没有发挥作用。

        let rol= req.usuario.rol;
        rol1=JSON.stringify({"rol":rol});
        let remove_spaces=JSON.parse(rol1).rol;
        remove_spaces=remove_spaces.replace(/ /g, "")
        console.log(remove_spaces,'Administrador General')
        //'Administrador General','Administrador General'
    

    在代码下面,我将原始代码的输出放入注释中。我希望他们能理解我。

    这是我原始代码的截图,我不知道它是否提供了正在发生的事情的线索。

    我想知道在这种情况下,在我的原始代码中,这个变量( rol )不允许删除空格或修改,即等于 Administrador General 为什么它会返回一个 false 相比之下呢?

    enter image description here

        let data={
          "usuario":{
          "nombres": "yeison velez",
          "apellidos": "velez guzman",
          "ultimo_inicio_sesion": null,
           "supervisor_array_id": [
            null
          ],
         "planta_id": null,
         "_id": "5fbc7cc705253c2da48209fe",
         "correo": "yeison_velez11@hotmail.com",
         "rol": "Administrador General",
         "__v": 0
          }
         }
        //let rol= req.usuario.rol; in my original code
        let rol= data.rol;
        rol1=JSON.stringify({"rol":rol});
        //output -> '{"rol":"Administrador General"}'
        let remove_spaces=JSON.parse(rol1).rol;
        //output ->'Administrador General'
        remove_spaces=remove_spaces.replace(/ /g, "")
        //output ->'Administrador General'
    
        console.log(remove_spaces,'Administrador General')
        //output ->Administrador General Administrador General
    
        console.log(typeof remove_spaces)
        //output ->string
        console.log(typeof 'Administrador General')
        //output ->string
        console.log(remove_spaces.length, 'Administrador General'.length)
        //output ->21 21
        console.log(remove_spaces=='Administrador General')
        //output ->false
        
       if (rol == 'Administrador General') {   //false
    0 回复  |  直到 5 年前
        1
  •  1
  •   coagmano    5 年前

    首先,代码片段中的注释并不反映实际发生的情况。 当我运行你的代码片段时

    未捕获的TypeError:无法访问属性“replace”, remove_空格未定义

    这是因为 rol 未定义,因为您正在访问 data.rol 而不是 data.usuario.rol .

    一旦我纠正了这一点,一切都按预期进行:

    let data = {
    "usuario": {
        "nombres": "yeison velez",
        "apellidos": "velez guzman",
        "ultimo_inicio_sesion": null,
        "supervisor_array_id": [
            null
        ],
        "planta_id": null,
        "_id": "5fbc7cc705253c2da48209fe",
        "correo": "yeison_velez11@hotmail.com",
        "rol": "Administrador General",
        "__v": 0
    }
    }
    //let rol= req.usuario.rol; in my original code
    let rol = data.usuario.rol;
    rol1 = JSON.stringify({ "rol": rol });
    //output -> '{"rol":"Administrador General"}'
    let remove_spaces = JSON.parse(rol1).rol;
    //output ->'Administrador General'
    remove_spaces = remove_spaces.replace(/ /g, "")
    //output ->'Administrador General'
    
    console.log(remove_spaces, 'Administrador General')
    //output ->Administrador General Administrador General
    
    console.log(typeof remove_spaces)
    //output ->string
    console.log(typeof 'Administrador General')
    //output ->string
    console.log(remove_spaces.length, 'Administrador General'.length)
    //output ->21 21
    console.log(remove_spaces == 'Administrador General')
    //output ->false

    我假设在你的原始代码中也有类似的问题,访问错误的东西