Why returned data from mongodb find() is not giving any data in api route? [duplicate]












-1
















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




What is wrong with this result? my res.send(dbResult) is not giving me any data. It is empty.



/* GET users listing. */
router.get('/', function(req, res, next) {

async function getData(){
try{
var dbResult = await mongodb.findData("contacts","iyaiContacts");
console.log('before dbResult');
res.send (dbResult);
}catch(error){
console.log("error getting contacts...", error.message);
}
}
getData();
});


Additional code for clarity. I have a db.js file that is used to handle the MongoDB connections and queries. Here is the Find Data function. Sorry I didn't realize I was being confusing until people asked questions.



const findData = (myColl, myDb) => {
try {
MongoClient.connect(
url,
function(err, client) {
client
.db(myDb)
.collection(myColl)
.find()
.sort({ nmFull: 1 })
.toArray(function(err, result) {
console.log("find() result...", result);
return result;
});
}
);
} catch (e) {
console.log(`findData() error message is...${e.message}`);
client.close(); // trying to close open db connection if it exist
throw e;
}
};









share|improve this question















marked as duplicate by CertainPerformance javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 20 at 9:07


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • mongodb.findData is valid ? Which package your using ?

    – Şivā SankĂr
    Jan 19 at 7:02











  • The word “async” before a function means one simple thing: a function always returns a promise.

    – chridam
    Jan 19 at 8:45











  • Which package your using to connect mongoDB ?. Usually it will be like .find(), what is .findData() mean ?

    – Şivā SankĂr
    Jan 19 at 9:09











  • I am using "mongodb": "3.1.10" driver for this connection.

    – Trewaters
    Jan 20 at 2:08
















-1
















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




What is wrong with this result? my res.send(dbResult) is not giving me any data. It is empty.



/* GET users listing. */
router.get('/', function(req, res, next) {

async function getData(){
try{
var dbResult = await mongodb.findData("contacts","iyaiContacts");
console.log('before dbResult');
res.send (dbResult);
}catch(error){
console.log("error getting contacts...", error.message);
}
}
getData();
});


Additional code for clarity. I have a db.js file that is used to handle the MongoDB connections and queries. Here is the Find Data function. Sorry I didn't realize I was being confusing until people asked questions.



const findData = (myColl, myDb) => {
try {
MongoClient.connect(
url,
function(err, client) {
client
.db(myDb)
.collection(myColl)
.find()
.sort({ nmFull: 1 })
.toArray(function(err, result) {
console.log("find() result...", result);
return result;
});
}
);
} catch (e) {
console.log(`findData() error message is...${e.message}`);
client.close(); // trying to close open db connection if it exist
throw e;
}
};









share|improve this question















marked as duplicate by CertainPerformance javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 20 at 9:07


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • mongodb.findData is valid ? Which package your using ?

    – Şivā SankĂr
    Jan 19 at 7:02











  • The word “async” before a function means one simple thing: a function always returns a promise.

    – chridam
    Jan 19 at 8:45











  • Which package your using to connect mongoDB ?. Usually it will be like .find(), what is .findData() mean ?

    – Şivā SankĂr
    Jan 19 at 9:09











  • I am using "mongodb": "3.1.10" driver for this connection.

    – Trewaters
    Jan 20 at 2:08














-1












-1








-1









This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




What is wrong with this result? my res.send(dbResult) is not giving me any data. It is empty.



/* GET users listing. */
router.get('/', function(req, res, next) {

async function getData(){
try{
var dbResult = await mongodb.findData("contacts","iyaiContacts");
console.log('before dbResult');
res.send (dbResult);
}catch(error){
console.log("error getting contacts...", error.message);
}
}
getData();
});


Additional code for clarity. I have a db.js file that is used to handle the MongoDB connections and queries. Here is the Find Data function. Sorry I didn't realize I was being confusing until people asked questions.



const findData = (myColl, myDb) => {
try {
MongoClient.connect(
url,
function(err, client) {
client
.db(myDb)
.collection(myColl)
.find()
.sort({ nmFull: 1 })
.toArray(function(err, result) {
console.log("find() result...", result);
return result;
});
}
);
} catch (e) {
console.log(`findData() error message is...${e.message}`);
client.close(); // trying to close open db connection if it exist
throw e;
}
};









share|improve this question

















This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers




What is wrong with this result? my res.send(dbResult) is not giving me any data. It is empty.



/* GET users listing. */
router.get('/', function(req, res, next) {

async function getData(){
try{
var dbResult = await mongodb.findData("contacts","iyaiContacts");
console.log('before dbResult');
res.send (dbResult);
}catch(error){
console.log("error getting contacts...", error.message);
}
}
getData();
});


Additional code for clarity. I have a db.js file that is used to handle the MongoDB connections and queries. Here is the Find Data function. Sorry I didn't realize I was being confusing until people asked questions.



const findData = (myColl, myDb) => {
try {
MongoClient.connect(
url,
function(err, client) {
client
.db(myDb)
.collection(myColl)
.find()
.sort({ nmFull: 1 })
.toArray(function(err, result) {
console.log("find() result...", result);
return result;
});
}
);
} catch (e) {
console.log(`findData() error message is...${e.message}`);
client.close(); // trying to close open db connection if it exist
throw e;
}
};




This question already has an answer here:




  • How do I return the response from an asynchronous call?

    33 answers








javascript mongodb






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 2:34







Trewaters

















asked Jan 19 at 6:28









TrewatersTrewaters

1711318




1711318




marked as duplicate by CertainPerformance javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 20 at 9:07


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by CertainPerformance javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 20 at 9:07


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • mongodb.findData is valid ? Which package your using ?

    – Şivā SankĂr
    Jan 19 at 7:02











  • The word “async” before a function means one simple thing: a function always returns a promise.

    – chridam
    Jan 19 at 8:45











  • Which package your using to connect mongoDB ?. Usually it will be like .find(), what is .findData() mean ?

    – Şivā SankĂr
    Jan 19 at 9:09











  • I am using "mongodb": "3.1.10" driver for this connection.

    – Trewaters
    Jan 20 at 2:08



















  • mongodb.findData is valid ? Which package your using ?

    – Şivā SankĂr
    Jan 19 at 7:02











  • The word “async” before a function means one simple thing: a function always returns a promise.

    – chridam
    Jan 19 at 8:45











  • Which package your using to connect mongoDB ?. Usually it will be like .find(), what is .findData() mean ?

    – Şivā SankĂr
    Jan 19 at 9:09











  • I am using "mongodb": "3.1.10" driver for this connection.

    – Trewaters
    Jan 20 at 2:08

















mongodb.findData is valid ? Which package your using ?

– Şivā SankĂr
Jan 19 at 7:02





mongodb.findData is valid ? Which package your using ?

– Şivā SankĂr
Jan 19 at 7:02













The word “async” before a function means one simple thing: a function always returns a promise.

– chridam
Jan 19 at 8:45





The word “async” before a function means one simple thing: a function always returns a promise.

– chridam
Jan 19 at 8:45













Which package your using to connect mongoDB ?. Usually it will be like .find(), what is .findData() mean ?

– Şivā SankĂr
Jan 19 at 9:09





Which package your using to connect mongoDB ?. Usually it will be like .find(), what is .findData() mean ?

– Şivā SankĂr
Jan 19 at 9:09













I am using "mongodb": "3.1.10" driver for this connection.

– Trewaters
Jan 20 at 2:08





I am using "mongodb": "3.1.10" driver for this connection.

– Trewaters
Jan 20 at 2:08












1 Answer
1






active

oldest

votes


















1














You are writing a backend service for your app that need to fetch data from your MongoDB. The problem is that your handling functions in synchronous manor — instead you need to handle as asynchronous.



// Quick fix for your code

const findData = (myColl, myDb) => {
return new Promise(function (resolve, reject) {
MongoClient.connect(url)
.then((client) => {
return client.db(myDb)
}).then((db) => {
return db.collection(myColl).find().sort({ nmFull: 1 })
.toArray((err, result) => { resolve(result); });
}).catch((err) => {
reject(err);
});
});
};


Here are some best practice to connect MongoDB on Node.js,




  • MongoClient or how to connect in a new and better way

  • Using MongoDB With Node.js






share|improve this answer


























  • Thanks for your help. That worked in my code. I didn't realize the db call was causing the issue. I thought it was in the API.

    – Trewaters
    Jan 20 at 14:32


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You are writing a backend service for your app that need to fetch data from your MongoDB. The problem is that your handling functions in synchronous manor — instead you need to handle as asynchronous.



// Quick fix for your code

const findData = (myColl, myDb) => {
return new Promise(function (resolve, reject) {
MongoClient.connect(url)
.then((client) => {
return client.db(myDb)
}).then((db) => {
return db.collection(myColl).find().sort({ nmFull: 1 })
.toArray((err, result) => { resolve(result); });
}).catch((err) => {
reject(err);
});
});
};


Here are some best practice to connect MongoDB on Node.js,




  • MongoClient or how to connect in a new and better way

  • Using MongoDB With Node.js






share|improve this answer


























  • Thanks for your help. That worked in my code. I didn't realize the db call was causing the issue. I thought it was in the API.

    – Trewaters
    Jan 20 at 14:32
















1














You are writing a backend service for your app that need to fetch data from your MongoDB. The problem is that your handling functions in synchronous manor — instead you need to handle as asynchronous.



// Quick fix for your code

const findData = (myColl, myDb) => {
return new Promise(function (resolve, reject) {
MongoClient.connect(url)
.then((client) => {
return client.db(myDb)
}).then((db) => {
return db.collection(myColl).find().sort({ nmFull: 1 })
.toArray((err, result) => { resolve(result); });
}).catch((err) => {
reject(err);
});
});
};


Here are some best practice to connect MongoDB on Node.js,




  • MongoClient or how to connect in a new and better way

  • Using MongoDB With Node.js






share|improve this answer


























  • Thanks for your help. That worked in my code. I didn't realize the db call was causing the issue. I thought it was in the API.

    – Trewaters
    Jan 20 at 14:32














1












1








1







You are writing a backend service for your app that need to fetch data from your MongoDB. The problem is that your handling functions in synchronous manor — instead you need to handle as asynchronous.



// Quick fix for your code

const findData = (myColl, myDb) => {
return new Promise(function (resolve, reject) {
MongoClient.connect(url)
.then((client) => {
return client.db(myDb)
}).then((db) => {
return db.collection(myColl).find().sort({ nmFull: 1 })
.toArray((err, result) => { resolve(result); });
}).catch((err) => {
reject(err);
});
});
};


Here are some best practice to connect MongoDB on Node.js,




  • MongoClient or how to connect in a new and better way

  • Using MongoDB With Node.js






share|improve this answer















You are writing a backend service for your app that need to fetch data from your MongoDB. The problem is that your handling functions in synchronous manor — instead you need to handle as asynchronous.



// Quick fix for your code

const findData = (myColl, myDb) => {
return new Promise(function (resolve, reject) {
MongoClient.connect(url)
.then((client) => {
return client.db(myDb)
}).then((db) => {
return db.collection(myColl).find().sort({ nmFull: 1 })
.toArray((err, result) => { resolve(result); });
}).catch((err) => {
reject(err);
});
});
};


Here are some best practice to connect MongoDB on Node.js,




  • MongoClient or how to connect in a new and better way

  • Using MongoDB With Node.js







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 20 at 4:42

























answered Jan 20 at 4:37









Şivā SankĂrŞivā SankĂr

558619




558619













  • Thanks for your help. That worked in my code. I didn't realize the db call was causing the issue. I thought it was in the API.

    – Trewaters
    Jan 20 at 14:32



















  • Thanks for your help. That worked in my code. I didn't realize the db call was causing the issue. I thought it was in the API.

    – Trewaters
    Jan 20 at 14:32

















Thanks for your help. That worked in my code. I didn't realize the db call was causing the issue. I thought it was in the API.

– Trewaters
Jan 20 at 14:32





Thanks for your help. That worked in my code. I didn't realize the db call was causing the issue. I thought it was in the API.

– Trewaters
Jan 20 at 14:32



Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre