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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현