Node.js stucked with var declaration

Issue

I have encode\decode up (React Next.sjs + Node.js Express). I’ve done almost everything I wanted. The only one problem I don’t know how to solve this is decoding. For decoding\encoding I’m using Cryptr library.

Here below my decode function code:


export const decodeBlog = async(req, res, next) => {
    const { text, secretkeyword } = req.body;
    const cryptr = new Cryptr(secretkeyword);
    const encrypted = text;
    let decrypted;   //The problem is here
    try {
        const decrypted = cryptr.decrypt(encrypted, secretkeyword);
        console.log("encrypted data", decrypted);
    } catch (err) {
        console.log(err);
        return res.status(400).json({message: "Incorrect Secret keyword"});
    }
    return res.status(200).json({message: decrypted}); 
};

enter image description here

Here in postman I’m getting an empty {} brackets.

But in node.js console I have console.log and it works:

enter image description here

I will be really appreciate if you help me. Thank you in advance!:)

Solution

You have two variables named decrypted:

  • one inside the try-catch block: this one is declared with const, it gets a value and is output with console.log
  • one outside the try-catch block: this one is declared with let and output with res.json, but it never gets a value.

Omit the word const inside the try-catch block. The let outside suffices.

Answered By – Heiko Theißen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published