Write a function to find all data from Mongo DB and pass to controller












0















I am new in Mean Stack. I install the express in MVC model.
I have a model named Data



let getAllData = ()=>{
Data.find({},(err,data)=>{
if(err) throw err;
var datas = {};

data.forEach((data)=>{
datas[data._id]=data;
});

console.log(datas);
});
}



module.exports = {
Data,
store,
getAllData
}


I want to call getAllData from a controller



var mongoose = require("mongoose");
var passport = require("passport");
var User = require("../models/User");
var DataModel = require("../models/Data");

var userController = {};



// Access to root page
userController.home = function(req, res) {
// res.render('index');
// res.send(Data.find);
DataModel.getAllData();
res.render('index',{//I want to send all the data to this view and view them from the page});
}


All I want is to make a function in the model to access all the data and show them in my view with a controller in middle. I am totally new and I could not exactly find the solution anywhere.



Thank you.










share|improve this question























  • Heads up, of it is a lot of data you'll want to get it in batches.

    – unflores
    Nov 25 '18 at 16:53
















0















I am new in Mean Stack. I install the express in MVC model.
I have a model named Data



let getAllData = ()=>{
Data.find({},(err,data)=>{
if(err) throw err;
var datas = {};

data.forEach((data)=>{
datas[data._id]=data;
});

console.log(datas);
});
}



module.exports = {
Data,
store,
getAllData
}


I want to call getAllData from a controller



var mongoose = require("mongoose");
var passport = require("passport");
var User = require("../models/User");
var DataModel = require("../models/Data");

var userController = {};



// Access to root page
userController.home = function(req, res) {
// res.render('index');
// res.send(Data.find);
DataModel.getAllData();
res.render('index',{//I want to send all the data to this view and view them from the page});
}


All I want is to make a function in the model to access all the data and show them in my view with a controller in middle. I am totally new and I could not exactly find the solution anywhere.



Thank you.










share|improve this question























  • Heads up, of it is a lot of data you'll want to get it in batches.

    – unflores
    Nov 25 '18 at 16:53














0












0








0








I am new in Mean Stack. I install the express in MVC model.
I have a model named Data



let getAllData = ()=>{
Data.find({},(err,data)=>{
if(err) throw err;
var datas = {};

data.forEach((data)=>{
datas[data._id]=data;
});

console.log(datas);
});
}



module.exports = {
Data,
store,
getAllData
}


I want to call getAllData from a controller



var mongoose = require("mongoose");
var passport = require("passport");
var User = require("../models/User");
var DataModel = require("../models/Data");

var userController = {};



// Access to root page
userController.home = function(req, res) {
// res.render('index');
// res.send(Data.find);
DataModel.getAllData();
res.render('index',{//I want to send all the data to this view and view them from the page});
}


All I want is to make a function in the model to access all the data and show them in my view with a controller in middle. I am totally new and I could not exactly find the solution anywhere.



Thank you.










share|improve this question














I am new in Mean Stack. I install the express in MVC model.
I have a model named Data



let getAllData = ()=>{
Data.find({},(err,data)=>{
if(err) throw err;
var datas = {};

data.forEach((data)=>{
datas[data._id]=data;
});

console.log(datas);
});
}



module.exports = {
Data,
store,
getAllData
}


I want to call getAllData from a controller



var mongoose = require("mongoose");
var passport = require("passport");
var User = require("../models/User");
var DataModel = require("../models/Data");

var userController = {};



// Access to root page
userController.home = function(req, res) {
// res.render('index');
// res.send(Data.find);
DataModel.getAllData();
res.render('index',{//I want to send all the data to this view and view them from the page});
}


All I want is to make a function in the model to access all the data and show them in my view with a controller in middle. I am totally new and I could not exactly find the solution anywhere.



Thank you.







node.js mongodb express mongoose mean-stack






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 16:04









Adnan SabbirAdnan Sabbir

75




75













  • Heads up, of it is a lot of data you'll want to get it in batches.

    – unflores
    Nov 25 '18 at 16:53



















  • Heads up, of it is a lot of data you'll want to get it in batches.

    – unflores
    Nov 25 '18 at 16:53

















Heads up, of it is a lot of data you'll want to get it in batches.

– unflores
Nov 25 '18 at 16:53





Heads up, of it is a lot of data you'll want to get it in batches.

– unflores
Nov 25 '18 at 16:53












1 Answer
1






active

oldest

votes


















0














You should use async await for this. This will allow you to call the function, wait for the promise to resolve and then respond the to request.



const getAllData = async () => {
return Data.find({})
.then(data => {})
.catch(error => {
throw error;
});
};

module.exports = {
Data,
store,
getAllData
};


When you define your function as async it will return a promise, so you can await its response.



var mongoose = require("mongoose");
var passport = require("passport");
var User = require("../models/User");
var DataModel = require("../models/Data");

var userController = {};

// Access to root page
userController.home = async function(req, res) {
// res.render('index');
// res.send(Data.find);
const data = await DataModel.getAllData();
res.render("index", data);
};





share|improve this answer


























  • Don't mix async-await with Promise syntax, use one or the other. The getAllData function can simply return the Promise returned by Data.find

    – Francisco Mateo
    Nov 25 '18 at 17:23











  • Why? What's the problem with it? Where would you catch errors?

    – Tom
    Nov 25 '18 at 18:28











  • It's like mixing .map() and for loops in same function. And for the answer you posted, you just throw the error anyways. Error handling in Express should be delegated to error middleware anyways: expressjs.com/en/guide/error-handling.html

    – Francisco Mateo
    Nov 25 '18 at 23:54











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%2f53469290%2fwrite-a-function-to-find-all-data-from-mongo-db-and-pass-to-controller%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You should use async await for this. This will allow you to call the function, wait for the promise to resolve and then respond the to request.



const getAllData = async () => {
return Data.find({})
.then(data => {})
.catch(error => {
throw error;
});
};

module.exports = {
Data,
store,
getAllData
};


When you define your function as async it will return a promise, so you can await its response.



var mongoose = require("mongoose");
var passport = require("passport");
var User = require("../models/User");
var DataModel = require("../models/Data");

var userController = {};

// Access to root page
userController.home = async function(req, res) {
// res.render('index');
// res.send(Data.find);
const data = await DataModel.getAllData();
res.render("index", data);
};





share|improve this answer


























  • Don't mix async-await with Promise syntax, use one or the other. The getAllData function can simply return the Promise returned by Data.find

    – Francisco Mateo
    Nov 25 '18 at 17:23











  • Why? What's the problem with it? Where would you catch errors?

    – Tom
    Nov 25 '18 at 18:28











  • It's like mixing .map() and for loops in same function. And for the answer you posted, you just throw the error anyways. Error handling in Express should be delegated to error middleware anyways: expressjs.com/en/guide/error-handling.html

    – Francisco Mateo
    Nov 25 '18 at 23:54
















0














You should use async await for this. This will allow you to call the function, wait for the promise to resolve and then respond the to request.



const getAllData = async () => {
return Data.find({})
.then(data => {})
.catch(error => {
throw error;
});
};

module.exports = {
Data,
store,
getAllData
};


When you define your function as async it will return a promise, so you can await its response.



var mongoose = require("mongoose");
var passport = require("passport");
var User = require("../models/User");
var DataModel = require("../models/Data");

var userController = {};

// Access to root page
userController.home = async function(req, res) {
// res.render('index');
// res.send(Data.find);
const data = await DataModel.getAllData();
res.render("index", data);
};





share|improve this answer


























  • Don't mix async-await with Promise syntax, use one or the other. The getAllData function can simply return the Promise returned by Data.find

    – Francisco Mateo
    Nov 25 '18 at 17:23











  • Why? What's the problem with it? Where would you catch errors?

    – Tom
    Nov 25 '18 at 18:28











  • It's like mixing .map() and for loops in same function. And for the answer you posted, you just throw the error anyways. Error handling in Express should be delegated to error middleware anyways: expressjs.com/en/guide/error-handling.html

    – Francisco Mateo
    Nov 25 '18 at 23:54














0












0








0







You should use async await for this. This will allow you to call the function, wait for the promise to resolve and then respond the to request.



const getAllData = async () => {
return Data.find({})
.then(data => {})
.catch(error => {
throw error;
});
};

module.exports = {
Data,
store,
getAllData
};


When you define your function as async it will return a promise, so you can await its response.



var mongoose = require("mongoose");
var passport = require("passport");
var User = require("../models/User");
var DataModel = require("../models/Data");

var userController = {};

// Access to root page
userController.home = async function(req, res) {
// res.render('index');
// res.send(Data.find);
const data = await DataModel.getAllData();
res.render("index", data);
};





share|improve this answer















You should use async await for this. This will allow you to call the function, wait for the promise to resolve and then respond the to request.



const getAllData = async () => {
return Data.find({})
.then(data => {})
.catch(error => {
throw error;
});
};

module.exports = {
Data,
store,
getAllData
};


When you define your function as async it will return a promise, so you can await its response.



var mongoose = require("mongoose");
var passport = require("passport");
var User = require("../models/User");
var DataModel = require("../models/Data");

var userController = {};

// Access to root page
userController.home = async function(req, res) {
// res.render('index');
// res.send(Data.find);
const data = await DataModel.getAllData();
res.render("index", data);
};






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 16:44

























answered Nov 25 '18 at 16:39









TomTom

1,28411024




1,28411024













  • Don't mix async-await with Promise syntax, use one or the other. The getAllData function can simply return the Promise returned by Data.find

    – Francisco Mateo
    Nov 25 '18 at 17:23











  • Why? What's the problem with it? Where would you catch errors?

    – Tom
    Nov 25 '18 at 18:28











  • It's like mixing .map() and for loops in same function. And for the answer you posted, you just throw the error anyways. Error handling in Express should be delegated to error middleware anyways: expressjs.com/en/guide/error-handling.html

    – Francisco Mateo
    Nov 25 '18 at 23:54



















  • Don't mix async-await with Promise syntax, use one or the other. The getAllData function can simply return the Promise returned by Data.find

    – Francisco Mateo
    Nov 25 '18 at 17:23











  • Why? What's the problem with it? Where would you catch errors?

    – Tom
    Nov 25 '18 at 18:28











  • It's like mixing .map() and for loops in same function. And for the answer you posted, you just throw the error anyways. Error handling in Express should be delegated to error middleware anyways: expressjs.com/en/guide/error-handling.html

    – Francisco Mateo
    Nov 25 '18 at 23:54

















Don't mix async-await with Promise syntax, use one or the other. The getAllData function can simply return the Promise returned by Data.find

– Francisco Mateo
Nov 25 '18 at 17:23





Don't mix async-await with Promise syntax, use one or the other. The getAllData function can simply return the Promise returned by Data.find

– Francisco Mateo
Nov 25 '18 at 17:23













Why? What's the problem with it? Where would you catch errors?

– Tom
Nov 25 '18 at 18:28





Why? What's the problem with it? Where would you catch errors?

– Tom
Nov 25 '18 at 18:28













It's like mixing .map() and for loops in same function. And for the answer you posted, you just throw the error anyways. Error handling in Express should be delegated to error middleware anyways: expressjs.com/en/guide/error-handling.html

– Francisco Mateo
Nov 25 '18 at 23:54





It's like mixing .map() and for loops in same function. And for the answer you posted, you just throw the error anyways. Error handling in Express should be delegated to error middleware anyways: expressjs.com/en/guide/error-handling.html

– Francisco Mateo
Nov 25 '18 at 23:54




















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%2f53469290%2fwrite-a-function-to-find-all-data-from-mongo-db-and-pass-to-controller%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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'