From 38ea9534c33ded9d11df7dcd06ef566d9490894b Mon Sep 17 00:00:00 2001 From: Aimi Date: Sat, 14 Dec 2019 22:53:10 +1000 Subject: [PATCH] Fix name Undefined bug --- main.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/main.js b/main.js index c6b33f9..ea6d530 100644 --- a/main.js +++ b/main.js @@ -1,30 +1,31 @@ -const express = require('express') -const app = express() +const express = require('express'); +const app = express(); // This is out apps base route. Any request to localhost:3000/ // is handled here -app.get('/', function(req, res) { +app.get('/', async function(req, res) { - // Start our reponse string - var response = 'Hello '; + // Start our response string + let response = 'Hello '; // Lets fetch our username for somewhere - getUserName(function(err, name) { + await getUserName(function(err, name) { if (err) { return res.status(500).send('Something is not right!') } // Join our strings - response = response + name; + response += name; + res.send(response); }); // Return response to client - res.send(response) + }) // This function will return our username to print -function getUserName(callback) { +async function getUserName(callback) { // Wrapping it in a timeout // to simulate if this is a database request setTimeout(function() { @@ -34,4 +35,4 @@ function getUserName(callback) { } // Port for server to start and listen on -app.listen(3000, () => console.log('Buggy app listening on port 3000!')) \ No newline at end of file +app.listen(3000, () => console.log('Buggy app listening on port 3000!')); \ No newline at end of file