Node/express Async function res.send appearing in console and not in pageHow to show data from mysql in nodejs with a refresh rateProper way to return JSON using node or ExpressNodejs with Mysql Database Return valuenode JS client vs serverMongoError: not master“Cannot GET /db/read/getall” accessing node.js express API using reverse proxy nginx on digitalocean dropletExpress req.body not WorkingWhat is best way to handle global connection of Mongodb in NodeJsI got an empty array in sub document array saving using mongoose ( MEAN stack)How can I use mongo changestreams and ws websockets in the same node.js file?

having problems with greek characters in a table using csvsimple

Scaling arrows.meta with tranform shape

Do multi-engine jets need all engines with equal age to reduce asymmetry in thrust and fuel consumption arising out of deterioration?

Do manacles provide any sort of in-game mechanical effect or condition?

How to understand payment due date for credit card?

Wrong Stamping of UK Visa

Is there an in-universe explanation given to the senior Imperial Navy Officers as to why Darth Vader serves Emperor Palpatine?

Why can't I identify major and minor chords?

Can I lend a small amount of my own money to a bank at the federal funds rate?

Board Chinese train at a different station (on-route)

Under GDPR, can I give permission once to allow everyone to store and process my data?

Why do motor drives have multiple bus capacitors of small value capacitance instead of a single bus capacitor of large value?

Why military weather satellites?

Why didn't Doc believe Marty was from the future?

Are spot colors limited and why CMYK mix is not treated same as spot color mix?

Why does the weaker C–H bond have a higher wavenumber than the C=O bond?

Group by consecutive index numbers

Idiomatic way to create an immutable and efficient class in C++?

Necessity of tenure for lifetime academic research

How to save money by shopping at a variety of grocery stores?

Codewars - Highest Scoring Word

What is this "opened" cube called?

Pen test results for web application include a file from a forbidden directory that is not even used or referenced

Is this homebrew "Faerie Fire Grenade" unbalanced?



Node/express Async function res.send appearing in console and not in page


How to show data from mysql in nodejs with a refresh rateProper way to return JSON using node or ExpressNodejs with Mysql Database Return valuenode JS client vs serverMongoError: not master“Cannot GET /db/read/getall” accessing node.js express API using reverse proxy nginx on digitalocean dropletExpress req.body not WorkingWhat is best way to handle global connection of Mongodb in NodeJsI got an empty array in sub document array saving using mongoose ( MEAN stack)How can I use mongo changestreams and ws websockets in the same node.js file?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have some code that gets some data from a mongo database.



The function inside the express app.get will show in the browser url but in this code the results is appearing only in the console and not in the web page, as it should.



Here is the code:



async function showdb() 

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, useNewUrlParser: true , function(err, db)
if (err) throw err;
var dbo = db.db('mydb');
dbo.collection('test').findOne(, function(err, result)
if (err) throw err;
console.log(result);
return result;
// res.send(result); // commented out
// db.close(); // commented out
);
);




Now the route:



app.get('/', (req, res) => 

showdb().then(result =>
res.send(result); // result is showing in the console
)

)


res.send(result); should show the data in the browser but it's showing it in the console.



How can I fix this?










share|improve this question
























  • What's in the console displaying? And what is its type?

    – Siamak Ferdos
    Mar 27 at 22:41











  • Its displaying this: _id: 5c9669ee4f401121adb78209, desc: 'This is data'

    – Kevin Porche
    Mar 27 at 22:52

















1















I have some code that gets some data from a mongo database.



The function inside the express app.get will show in the browser url but in this code the results is appearing only in the console and not in the web page, as it should.



Here is the code:



async function showdb() 

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, useNewUrlParser: true , function(err, db)
if (err) throw err;
var dbo = db.db('mydb');
dbo.collection('test').findOne(, function(err, result)
if (err) throw err;
console.log(result);
return result;
// res.send(result); // commented out
// db.close(); // commented out
);
);




Now the route:



app.get('/', (req, res) => 

showdb().then(result =>
res.send(result); // result is showing in the console
)

)


res.send(result); should show the data in the browser but it's showing it in the console.



How can I fix this?










share|improve this question
























  • What's in the console displaying? And what is its type?

    – Siamak Ferdos
    Mar 27 at 22:41











  • Its displaying this: _id: 5c9669ee4f401121adb78209, desc: 'This is data'

    – Kevin Porche
    Mar 27 at 22:52













1












1








1


1






I have some code that gets some data from a mongo database.



The function inside the express app.get will show in the browser url but in this code the results is appearing only in the console and not in the web page, as it should.



Here is the code:



async function showdb() 

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, useNewUrlParser: true , function(err, db)
if (err) throw err;
var dbo = db.db('mydb');
dbo.collection('test').findOne(, function(err, result)
if (err) throw err;
console.log(result);
return result;
// res.send(result); // commented out
// db.close(); // commented out
);
);




Now the route:



app.get('/', (req, res) => 

showdb().then(result =>
res.send(result); // result is showing in the console
)

)


res.send(result); should show the data in the browser but it's showing it in the console.



How can I fix this?










share|improve this question














I have some code that gets some data from a mongo database.



The function inside the express app.get will show in the browser url but in this code the results is appearing only in the console and not in the web page, as it should.



Here is the code:



async function showdb() 

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url, useNewUrlParser: true , function(err, db)
if (err) throw err;
var dbo = db.db('mydb');
dbo.collection('test').findOne(, function(err, result)
if (err) throw err;
console.log(result);
return result;
// res.send(result); // commented out
// db.close(); // commented out
);
);




Now the route:



app.get('/', (req, res) => 

showdb().then(result =>
res.send(result); // result is showing in the console
)

)


res.send(result); should show the data in the browser but it's showing it in the console.



How can I fix this?







node.js mongodb express async-await






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 22:15









Kevin PorcheKevin Porche

1348 bronze badges




1348 bronze badges















  • What's in the console displaying? And what is its type?

    – Siamak Ferdos
    Mar 27 at 22:41











  • Its displaying this: _id: 5c9669ee4f401121adb78209, desc: 'This is data'

    – Kevin Porche
    Mar 27 at 22:52

















  • What's in the console displaying? And what is its type?

    – Siamak Ferdos
    Mar 27 at 22:41











  • Its displaying this: _id: 5c9669ee4f401121adb78209, desc: 'This is data'

    – Kevin Porche
    Mar 27 at 22:52
















What's in the console displaying? And what is its type?

– Siamak Ferdos
Mar 27 at 22:41





What's in the console displaying? And what is its type?

– Siamak Ferdos
Mar 27 at 22:41













Its displaying this: _id: 5c9669ee4f401121adb78209, desc: 'This is data'

– Kevin Porche
Mar 27 at 22:52





Its displaying this: _id: 5c9669ee4f401121adb78209, desc: 'This is data'

– Kevin Porche
Mar 27 at 22:52












2 Answers
2






active

oldest

votes


















1















Both MongoClient.connect() and collection.findOne()
functions return a Promise, so you can chain them without using callbacks with less code:



function showdb() 

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

return MongoClient.connect(url, useNewUrlParser: true )
.then((db) =>
return db.db('mydb').collection('test').findOne();
);



Now the showdb function will return a Promise to your route.






share|improve this answer
































    0















    You didn't included the Port on which server is listening. Try to add below code this might help you



    var app = express();
    app.set('port', 3000);
    var server= app.listen(app.get('port'), function()



    var port = server.address().port;
    console.log(" Iam at Port:" + " " + port);


    );





    share|improve this answer

























    • That's not the case ... that code is at the bottom of the page. If I put th showdb code inside the app.get it works fine. It looks like something to do with the async?

      – Kevin Porche
      Mar 27 at 22:29













    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387292%2fnode-express-async-function-res-send-appearing-in-console-and-not-in-page%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1















    Both MongoClient.connect() and collection.findOne()
    functions return a Promise, so you can chain them without using callbacks with less code:



    function showdb() 

    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/";

    return MongoClient.connect(url, useNewUrlParser: true )
    .then((db) =>
    return db.db('mydb').collection('test').findOne();
    );



    Now the showdb function will return a Promise to your route.






    share|improve this answer





























      1















      Both MongoClient.connect() and collection.findOne()
      functions return a Promise, so you can chain them without using callbacks with less code:



      function showdb() 

      var MongoClient = require('mongodb').MongoClient;
      var url = "mongodb://localhost:27017/";

      return MongoClient.connect(url, useNewUrlParser: true )
      .then((db) =>
      return db.db('mydb').collection('test').findOne();
      );



      Now the showdb function will return a Promise to your route.






      share|improve this answer



























        1














        1










        1









        Both MongoClient.connect() and collection.findOne()
        functions return a Promise, so you can chain them without using callbacks with less code:



        function showdb() 

        var MongoClient = require('mongodb').MongoClient;
        var url = "mongodb://localhost:27017/";

        return MongoClient.connect(url, useNewUrlParser: true )
        .then((db) =>
        return db.db('mydb').collection('test').findOne();
        );



        Now the showdb function will return a Promise to your route.






        share|improve this answer













        Both MongoClient.connect() and collection.findOne()
        functions return a Promise, so you can chain them without using callbacks with less code:



        function showdb() 

        var MongoClient = require('mongodb').MongoClient;
        var url = "mongodb://localhost:27017/";

        return MongoClient.connect(url, useNewUrlParser: true )
        .then((db) =>
        return db.db('mydb').collection('test').findOne();
        );



        Now the showdb function will return a Promise to your route.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 22:52









        lifeisfoolifeisfoo

        7,8303 gold badges46 silver badges74 bronze badges




        7,8303 gold badges46 silver badges74 bronze badges


























            0















            You didn't included the Port on which server is listening. Try to add below code this might help you



            var app = express();
            app.set('port', 3000);
            var server= app.listen(app.get('port'), function()



            var port = server.address().port;
            console.log(" Iam at Port:" + " " + port);


            );





            share|improve this answer

























            • That's not the case ... that code is at the bottom of the page. If I put th showdb code inside the app.get it works fine. It looks like something to do with the async?

              – Kevin Porche
              Mar 27 at 22:29















            0















            You didn't included the Port on which server is listening. Try to add below code this might help you



            var app = express();
            app.set('port', 3000);
            var server= app.listen(app.get('port'), function()



            var port = server.address().port;
            console.log(" Iam at Port:" + " " + port);


            );





            share|improve this answer

























            • That's not the case ... that code is at the bottom of the page. If I put th showdb code inside the app.get it works fine. It looks like something to do with the async?

              – Kevin Porche
              Mar 27 at 22:29













            0














            0










            0









            You didn't included the Port on which server is listening. Try to add below code this might help you



            var app = express();
            app.set('port', 3000);
            var server= app.listen(app.get('port'), function()



            var port = server.address().port;
            console.log(" Iam at Port:" + " " + port);


            );





            share|improve this answer













            You didn't included the Port on which server is listening. Try to add below code this might help you



            var app = express();
            app.set('port', 3000);
            var server= app.listen(app.get('port'), function()



            var port = server.address().port;
            console.log(" Iam at Port:" + " " + port);


            );






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 27 at 22:21









            AlexAlex

            184 bronze badges




            184 bronze badges















            • That's not the case ... that code is at the bottom of the page. If I put th showdb code inside the app.get it works fine. It looks like something to do with the async?

              – Kevin Porche
              Mar 27 at 22:29

















            • That's not the case ... that code is at the bottom of the page. If I put th showdb code inside the app.get it works fine. It looks like something to do with the async?

              – Kevin Porche
              Mar 27 at 22:29
















            That's not the case ... that code is at the bottom of the page. If I put th showdb code inside the app.get it works fine. It looks like something to do with the async?

            – Kevin Porche
            Mar 27 at 22:29





            That's not the case ... that code is at the bottom of the page. If I put th showdb code inside the app.get it works fine. It looks like something to do with the async?

            – Kevin Porche
            Mar 27 at 22:29

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387292%2fnode-express-async-function-res-send-appearing-in-console-and-not-in-page%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript