trying to get the array result for mongodb in expressjs2019 Community Moderator ElectionNode.js MongoDB collection.find().toArray returns nothingHow do you get a timestamp in JavaScript?How do I check if an array includes an object in JavaScript?How to append something to an array?Get the current URL with JavaScript?Loop through an array in JavaScriptHow to query MongoDB with “like”?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?I got an empty array in sub document array saving using mongoose ( MEAN stack)
Jem'Hadar, something strange about their life expectancy
Turning a hard to access nut?
What (if any) is the reason to buy in small local stores?
Extraneous elements in "Europe countries" list
Why didn’t Eve recognize the little cockroach as a living organism?
How are passwords stolen from companies if they only store hashes?
Does convergence of polynomials imply that of its coefficients?
Why is "la Gestapo" feminine?
Do native speakers use "ultima" and "proxima" frequently in spoken English?
Have the tides ever turned twice on any open problem?
Determine voltage drop over 10G resistors with cheap multimeter
Symbolism of 18 Journeyers
Unfrosted light bulb
What is the reasoning behind standardization (dividing by standard deviation)?
How do you justify more code being written by following clean code practices?
Weird lines in Microsoft Word
How can I create URL shortcuts/redirects for task/diff IDs in Phabricator?
Imaginary part of expression too difficult to calculate
Have any astronauts/cosmonauts died in space?
Recursively updating the MLE as new observations stream in
Emojional cryptic crossword
Error in master's thesis, I do not know what to do
Should a narrator ever describe things based on a characters view instead of fact?
Do people actually use the word "kaputt" in conversation?
trying to get the array result for mongodb in expressjs
2019 Community Moderator ElectionNode.js MongoDB collection.find().toArray returns nothingHow do you get a timestamp in JavaScript?How do I check if an array includes an object in JavaScript?How to append something to an array?Get the current URL with JavaScript?Loop through an array in JavaScriptHow to query MongoDB with “like”?How to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?I got an empty array in sub document array saving using mongoose ( MEAN stack)
I'm trying to get a result from MongoDB database return as a response for my GET Request when I make the call I'm getting undefined in the response, I'm able to see the result in console log on the database side of the code. I'm feeling like it might have something to do with it being an array
MongoClient = require('mongodb'),
mongodburl = "mongodb://localhost:27017",
mongo = require('mongodb'),
assert = require('assert');
let method=
method.insertdata=function(data,collectionpara)
MongoClient.connect(mongodburl, function (err, db)
assert.equal(null, err);
let dbo = db.db("hor-usData");
let myobj = data;
dbo.collection(collectionpara).insert(myobj, function (err, result)
assert.equal(null, err);
console.log("insert data");
db.close();
)
)
method.deleteRecords=function(collectionpara)
MongoClient.connect(mongodburl, function (err, db)
assert.equal(null, err);
let dbo = db.db("hor-usData");
var myquery =
title: /^B/
;
dbo.collection(collectionpara).deleteMany(myquery, function (err, obj)
if (err) throw err;
console.log(obj.result.n + " document(s) deleted");
db.close();
)
)
method.getdata=function(collectionpara)
MongoClient.connect(mongodburl, function(err, db)
if (err) throw err;
let dbo = db.db("hor-usData");
dbo.collection(collectionpara).find().toArray(function(err, result)
if (err)
return reject(err)
console.log('result',result);
return resolve(result);
);
);
module.exports = method;
and my router code
let assert = require('assert'),
express = require('express'),
router = express(),
swaggerUi = require('swagger-ui-express'),
database=require('../databaseCon'),
// swaggerDocument = require('./swagger.json'),
utils = require('../utils/utils');
var port = process.env.PORT || 3000;
//get donedeal
router.get('/getDonedeal', function (req, res, next)
let donedealResult = []
donedealResult=database.getdata('donedeal');
res.send(donedealResult);
);
//get carzone
router.get('/getCarzone', function (req, res, next)
let carzoneResult = [];
carzoneResult=database.getdata('carzone');
res.send(console.log(carzoneResult));
);
router.get('/cars', function (req, res, next)
res.router('Mainrouter')
);
router.get('/getCarzone', function (req, res, next)
var resultArray = []
);
router.listen(port);
//app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
//app.use('/api/v1', router);
module.exports = router;
javascript node.js mongodb express mongoose
add a comment |
I'm trying to get a result from MongoDB database return as a response for my GET Request when I make the call I'm getting undefined in the response, I'm able to see the result in console log on the database side of the code. I'm feeling like it might have something to do with it being an array
MongoClient = require('mongodb'),
mongodburl = "mongodb://localhost:27017",
mongo = require('mongodb'),
assert = require('assert');
let method=
method.insertdata=function(data,collectionpara)
MongoClient.connect(mongodburl, function (err, db)
assert.equal(null, err);
let dbo = db.db("hor-usData");
let myobj = data;
dbo.collection(collectionpara).insert(myobj, function (err, result)
assert.equal(null, err);
console.log("insert data");
db.close();
)
)
method.deleteRecords=function(collectionpara)
MongoClient.connect(mongodburl, function (err, db)
assert.equal(null, err);
let dbo = db.db("hor-usData");
var myquery =
title: /^B/
;
dbo.collection(collectionpara).deleteMany(myquery, function (err, obj)
if (err) throw err;
console.log(obj.result.n + " document(s) deleted");
db.close();
)
)
method.getdata=function(collectionpara)
MongoClient.connect(mongodburl, function(err, db)
if (err) throw err;
let dbo = db.db("hor-usData");
dbo.collection(collectionpara).find().toArray(function(err, result)
if (err)
return reject(err)
console.log('result',result);
return resolve(result);
);
);
module.exports = method;
and my router code
let assert = require('assert'),
express = require('express'),
router = express(),
swaggerUi = require('swagger-ui-express'),
database=require('../databaseCon'),
// swaggerDocument = require('./swagger.json'),
utils = require('../utils/utils');
var port = process.env.PORT || 3000;
//get donedeal
router.get('/getDonedeal', function (req, res, next)
let donedealResult = []
donedealResult=database.getdata('donedeal');
res.send(donedealResult);
);
//get carzone
router.get('/getCarzone', function (req, res, next)
let carzoneResult = [];
carzoneResult=database.getdata('carzone');
res.send(console.log(carzoneResult));
);
router.get('/cars', function (req, res, next)
res.router('Mainrouter')
);
router.get('/getCarzone', function (req, res, next)
var resultArray = []
);
router.listen(port);
//app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
//app.use('/api/v1', router);
module.exports = router;
javascript node.js mongodb express mongoose
1
It has already been answered on this post : Node.js MongoDB collection.find().toArray returns nothing
– Sense
Mar 5 at 14:48
the problem I'm having seems to be more on the express side, I can data no problem but getting the data to show when I make API call is not working
– soad666p
Mar 5 at 15:39
1
@Sense is right: your answer is in the validated answer of the linked post. Yourmethod.getdata
should return a promise (instead of implicitly returningundefined
), and this promise should be resolved withresult
, insidetoArray
's callback.
– Stock Overflaw
Mar 5 at 16:42
@Sense, I update the database code, I'm still getting the same error on the express side when I made the /getDonedeal call it showing as undefine
– soad666p
Mar 6 at 23:29
You did not create any Promise in yourgetdata
function, so you can'tresolve
orreject
anything. Please read carefully the link I provided earlier.
– Sense
Mar 7 at 9:13
add a comment |
I'm trying to get a result from MongoDB database return as a response for my GET Request when I make the call I'm getting undefined in the response, I'm able to see the result in console log on the database side of the code. I'm feeling like it might have something to do with it being an array
MongoClient = require('mongodb'),
mongodburl = "mongodb://localhost:27017",
mongo = require('mongodb'),
assert = require('assert');
let method=
method.insertdata=function(data,collectionpara)
MongoClient.connect(mongodburl, function (err, db)
assert.equal(null, err);
let dbo = db.db("hor-usData");
let myobj = data;
dbo.collection(collectionpara).insert(myobj, function (err, result)
assert.equal(null, err);
console.log("insert data");
db.close();
)
)
method.deleteRecords=function(collectionpara)
MongoClient.connect(mongodburl, function (err, db)
assert.equal(null, err);
let dbo = db.db("hor-usData");
var myquery =
title: /^B/
;
dbo.collection(collectionpara).deleteMany(myquery, function (err, obj)
if (err) throw err;
console.log(obj.result.n + " document(s) deleted");
db.close();
)
)
method.getdata=function(collectionpara)
MongoClient.connect(mongodburl, function(err, db)
if (err) throw err;
let dbo = db.db("hor-usData");
dbo.collection(collectionpara).find().toArray(function(err, result)
if (err)
return reject(err)
console.log('result',result);
return resolve(result);
);
);
module.exports = method;
and my router code
let assert = require('assert'),
express = require('express'),
router = express(),
swaggerUi = require('swagger-ui-express'),
database=require('../databaseCon'),
// swaggerDocument = require('./swagger.json'),
utils = require('../utils/utils');
var port = process.env.PORT || 3000;
//get donedeal
router.get('/getDonedeal', function (req, res, next)
let donedealResult = []
donedealResult=database.getdata('donedeal');
res.send(donedealResult);
);
//get carzone
router.get('/getCarzone', function (req, res, next)
let carzoneResult = [];
carzoneResult=database.getdata('carzone');
res.send(console.log(carzoneResult));
);
router.get('/cars', function (req, res, next)
res.router('Mainrouter')
);
router.get('/getCarzone', function (req, res, next)
var resultArray = []
);
router.listen(port);
//app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
//app.use('/api/v1', router);
module.exports = router;
javascript node.js mongodb express mongoose
I'm trying to get a result from MongoDB database return as a response for my GET Request when I make the call I'm getting undefined in the response, I'm able to see the result in console log on the database side of the code. I'm feeling like it might have something to do with it being an array
MongoClient = require('mongodb'),
mongodburl = "mongodb://localhost:27017",
mongo = require('mongodb'),
assert = require('assert');
let method=
method.insertdata=function(data,collectionpara)
MongoClient.connect(mongodburl, function (err, db)
assert.equal(null, err);
let dbo = db.db("hor-usData");
let myobj = data;
dbo.collection(collectionpara).insert(myobj, function (err, result)
assert.equal(null, err);
console.log("insert data");
db.close();
)
)
method.deleteRecords=function(collectionpara)
MongoClient.connect(mongodburl, function (err, db)
assert.equal(null, err);
let dbo = db.db("hor-usData");
var myquery =
title: /^B/
;
dbo.collection(collectionpara).deleteMany(myquery, function (err, obj)
if (err) throw err;
console.log(obj.result.n + " document(s) deleted");
db.close();
)
)
method.getdata=function(collectionpara)
MongoClient.connect(mongodburl, function(err, db)
if (err) throw err;
let dbo = db.db("hor-usData");
dbo.collection(collectionpara).find().toArray(function(err, result)
if (err)
return reject(err)
console.log('result',result);
return resolve(result);
);
);
module.exports = method;
and my router code
let assert = require('assert'),
express = require('express'),
router = express(),
swaggerUi = require('swagger-ui-express'),
database=require('../databaseCon'),
// swaggerDocument = require('./swagger.json'),
utils = require('../utils/utils');
var port = process.env.PORT || 3000;
//get donedeal
router.get('/getDonedeal', function (req, res, next)
let donedealResult = []
donedealResult=database.getdata('donedeal');
res.send(donedealResult);
);
//get carzone
router.get('/getCarzone', function (req, res, next)
let carzoneResult = [];
carzoneResult=database.getdata('carzone');
res.send(console.log(carzoneResult));
);
router.get('/cars', function (req, res, next)
res.router('Mainrouter')
);
router.get('/getCarzone', function (req, res, next)
var resultArray = []
);
router.listen(port);
//app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
//app.use('/api/v1', router);
module.exports = router;
javascript node.js mongodb express mongoose
javascript node.js mongodb express mongoose
edited Mar 6 at 23:23
soad666p
asked Mar 5 at 14:39
soad666psoad666p
14
14
1
It has already been answered on this post : Node.js MongoDB collection.find().toArray returns nothing
– Sense
Mar 5 at 14:48
the problem I'm having seems to be more on the express side, I can data no problem but getting the data to show when I make API call is not working
– soad666p
Mar 5 at 15:39
1
@Sense is right: your answer is in the validated answer of the linked post. Yourmethod.getdata
should return a promise (instead of implicitly returningundefined
), and this promise should be resolved withresult
, insidetoArray
's callback.
– Stock Overflaw
Mar 5 at 16:42
@Sense, I update the database code, I'm still getting the same error on the express side when I made the /getDonedeal call it showing as undefine
– soad666p
Mar 6 at 23:29
You did not create any Promise in yourgetdata
function, so you can'tresolve
orreject
anything. Please read carefully the link I provided earlier.
– Sense
Mar 7 at 9:13
add a comment |
1
It has already been answered on this post : Node.js MongoDB collection.find().toArray returns nothing
– Sense
Mar 5 at 14:48
the problem I'm having seems to be more on the express side, I can data no problem but getting the data to show when I make API call is not working
– soad666p
Mar 5 at 15:39
1
@Sense is right: your answer is in the validated answer of the linked post. Yourmethod.getdata
should return a promise (instead of implicitly returningundefined
), and this promise should be resolved withresult
, insidetoArray
's callback.
– Stock Overflaw
Mar 5 at 16:42
@Sense, I update the database code, I'm still getting the same error on the express side when I made the /getDonedeal call it showing as undefine
– soad666p
Mar 6 at 23:29
You did not create any Promise in yourgetdata
function, so you can'tresolve
orreject
anything. Please read carefully the link I provided earlier.
– Sense
Mar 7 at 9:13
1
1
It has already been answered on this post : Node.js MongoDB collection.find().toArray returns nothing
– Sense
Mar 5 at 14:48
It has already been answered on this post : Node.js MongoDB collection.find().toArray returns nothing
– Sense
Mar 5 at 14:48
the problem I'm having seems to be more on the express side, I can data no problem but getting the data to show when I make API call is not working
– soad666p
Mar 5 at 15:39
the problem I'm having seems to be more on the express side, I can data no problem but getting the data to show when I make API call is not working
– soad666p
Mar 5 at 15:39
1
1
@Sense is right: your answer is in the validated answer of the linked post. Your
method.getdata
should return a promise (instead of implicitly returning undefined
), and this promise should be resolved with result
, inside toArray
's callback.– Stock Overflaw
Mar 5 at 16:42
@Sense is right: your answer is in the validated answer of the linked post. Your
method.getdata
should return a promise (instead of implicitly returning undefined
), and this promise should be resolved with result
, inside toArray
's callback.– Stock Overflaw
Mar 5 at 16:42
@Sense, I update the database code, I'm still getting the same error on the express side when I made the /getDonedeal call it showing as undefine
– soad666p
Mar 6 at 23:29
@Sense, I update the database code, I'm still getting the same error on the express side when I made the /getDonedeal call it showing as undefine
– soad666p
Mar 6 at 23:29
You did not create any Promise in your
getdata
function, so you can't resolve
or reject
anything. Please read carefully the link I provided earlier.– Sense
Mar 7 at 9:13
You did not create any Promise in your
getdata
function, so you can't resolve
or reject
anything. Please read carefully the link I provided earlier.– Sense
Mar 7 at 9:13
add a comment |
0
active
oldest
votes
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%2f55005309%2ftrying-to-get-the-array-result-for-mongodb-in-expressjs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f55005309%2ftrying-to-get-the-array-result-for-mongodb-in-expressjs%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
1
It has already been answered on this post : Node.js MongoDB collection.find().toArray returns nothing
– Sense
Mar 5 at 14:48
the problem I'm having seems to be more on the express side, I can data no problem but getting the data to show when I make API call is not working
– soad666p
Mar 5 at 15:39
1
@Sense is right: your answer is in the validated answer of the linked post. Your
method.getdata
should return a promise (instead of implicitly returningundefined
), and this promise should be resolved withresult
, insidetoArray
's callback.– Stock Overflaw
Mar 5 at 16:42
@Sense, I update the database code, I'm still getting the same error on the express side when I made the /getDonedeal call it showing as undefine
– soad666p
Mar 6 at 23:29
You did not create any Promise in your
getdata
function, so you can'tresolve
orreject
anything. Please read carefully the link I provided earlier.– Sense
Mar 7 at 9:13