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;
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
add a comment |
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
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
add a comment |
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
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
node.js mongodb express async-await
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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);
);
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 27 at 22:52
lifeisfoolifeisfoo
7,8303 gold badges46 silver badges74 bronze badges
7,8303 gold badges46 silver badges74 bronze badges
add a comment |
add a comment |
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);
);
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
add a comment |
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);
);
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
add a comment |
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);
);
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);
);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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