Write a function to find all data from Mongo DB and pass to controller
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
add a comment |
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
Heads up, of it is a lot of data you'll want to get it in batches.
– unflores
Nov 25 '18 at 16:53
add a comment |
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
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
node.js mongodb express mongoose mean-stack
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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);
};
Don't mixasync-await
withPromise
syntax, use one or the other. ThegetAllData
function can simply return thePromise
returned byData.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()
andfor
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
add a comment |
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%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
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);
};
Don't mixasync-await
withPromise
syntax, use one or the other. ThegetAllData
function can simply return thePromise
returned byData.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()
andfor
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
add a comment |
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);
};
Don't mixasync-await
withPromise
syntax, use one or the other. ThegetAllData
function can simply return thePromise
returned byData.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()
andfor
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
add a comment |
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);
};
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);
};
edited Nov 25 '18 at 16:44
answered Nov 25 '18 at 16:39
TomTom
1,28411024
1,28411024
Don't mixasync-await
withPromise
syntax, use one or the other. ThegetAllData
function can simply return thePromise
returned byData.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()
andfor
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
add a comment |
Don't mixasync-await
withPromise
syntax, use one or the other. ThegetAllData
function can simply return thePromise
returned byData.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()
andfor
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
add a comment |
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%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
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
Heads up, of it is a lot of data you'll want to get it in batches.
– unflores
Nov 25 '18 at 16:53