Why returned data from mongodb find() is not giving any data in api route? [duplicate]
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;
}
};
javascript mongodb
marked as duplicate by CertainPerformance
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.
add a comment |
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;
}
};
javascript mongodb
marked as duplicate by CertainPerformance
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
add a comment |
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;
}
};
javascript mongodb
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
javascript mongodb
edited Jan 20 at 2:34
Trewaters
asked Jan 19 at 6:28
TrewatersTrewaters
1711318
1711318
marked as duplicate by CertainPerformance
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
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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