1

I had been coding a webapp and in his backend there's a function to create users. I need to use the values that are returned by that function, but how can I handle those values?

The function code:

module.exports = function(username, socialname, password) {
    var hash = "PASSWORD";

    var userId = nanoid(8);

    var sucessMessage = {
        sucesso: true,
        message:"User created",
        userId: userId
    }

    return sucessMessage; // Returning undefined when the function is displayed on console
};

And in mine server.js file, where I'm handling the function:

module.exports.userCreate = function (username, socialname, password) {
    userCreate(username, socialname, password);
};

So, in the index.js I want to catch that returning value and display it on the console, how can I do that?

app.post("/user/create", (req, res) => { 
    var username = req.body.username;
    var socialname = req.body.socialname;
    var password = req.body.password;

    const createdUser = userCommands.userCreate(username, socialname, password);
    console.log(createdUser ) // undefined
});
Igor Duca
  • 23
  • 3
  • Try assigning the value of `userCommands.userCreate(username, socialname, password)` to a variable, like `const createdUser = userCommands.userCreate(username, socialname, password)` and then just `console.log(createdUser)`. – nick-w-nick Aug 18 '21 at 15:29
  • I already tried that, the function is returning me undefined – Igor Duca Aug 18 '21 at 15:48
  • You need to also return the value from where you've redefined the function in `server.js`. Add the `return` keyword in front of `userCreate(username, socialname, password);`. – nick-w-nick Aug 18 '21 at 16:03
  • `function is returning me undefined` - because you are not returning anything. Javascript's default value for "nothing" is `undefined` -- `module.exports.userCreate = /* function that returns nothing/undefined */` – slebetman Aug 18 '21 at 16:25
  • Thank y'all, guys, now it is working – Igor Duca Aug 18 '21 at 18:13

0 Answers0