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)










0















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;









share|improve this question



















  • 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 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











  • 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















0















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;









share|improve this question



















  • 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 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











  • 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













0












0








0








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;









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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. 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











  • 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












  • 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 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











  • 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







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












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
);



);













draft saved

draft discarded


















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















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%2f55005309%2ftrying-to-get-the-array-result-for-mongodb-in-expressjs%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

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved