-1

so i an have express server, and i want to use data that I get outside of post function or in other post functions here is the code

app.post('/bg-login', (req, res) => {
            var user;
            req.body.email;
            req.body.password;
            var email1 = req.body.email;
            const path = './Databases/User/' + email1 + '.json';
            if (fs.existsSync(path)) {
                try {
                    // Note that jsonString will be a <Buffer> since we did not specify an
                    // encoding type for the file. But it'll still work because JSON.parse() will
                    // use <Buffer>.toString().
                } catch (err) {
                    return;
                }
                var user1 = fs.readFileSync('./Databases/User/1.json');
                var user = JSON.parse(user1)
            } else {
                res.redirect("/login-e1");
            }
            console.log(user);

Error: user is not defined, so how could I get this variable ( user) to work outside POST function

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • It isn't outside the function you pass to `post`. Did you forgot a `})` in there? – Quentin Aug 17 '22 at 09:20
  • This is [probably a duplicate of this](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) but since the code won't even compile it isn't entirely clear. – Quentin Aug 17 '22 at 09:21
  • Also, it looks like you are trying to write a log in system but with the approach you seem to be trying to take, you'll end up with a situation where if **one** user logs in then **everybody accessing the site** will be logged in as that user. You need to investigate **sessions**. – Quentin Aug 17 '22 at 09:23

1 Answers1

-2

You can just define the variable "user" in the global scope instead of the function scope.

  • 1
    [no, they can't](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron). – Quentin Aug 17 '22 at 09:21
  • You're right. I though it was a regular function, not a callback. But he can go with global.user, although he might run to problems when his user variable value is being replaced by some other value. He could just store the user in the database and store some kind of ID in a session or the user object itself if that's the case. Some admin sites allow only one user at a time, if that's the case global.user should work awesomely, one example is the most routers' administration page. – Constantinos Petrakis Aug 17 '22 at 09:31
  • so if i understand i have to define variable like " const globa.user = ..."?. tnx bdw for anwser – Svit SP2009 Aug 17 '22 at 09:38
  • global, is an already defined object in Javascript. So you just define your variables as attributes to that object. For example, `global.user = JSON.parse(myStringObject)` Although be cautious with that option if your site is visited by more than 1 user at a time, eventually you will run to problems with that option. – Constantinos Petrakis Aug 17 '22 at 09:43
  • it is desplaying [object Object] this am I doing smt wrong global.user = JSON.parse(user1); and this oudsite that function res.send("Hello " + global.user); – Svit SP2009 Aug 17 '22 at 10:17