I want to call function from my object in express route. This function should call mongoose query, then run next, next etc. - all needed operations.
Here is my example route:
var MailSender = require('../../libs/mailer');
router.get('/mailer/test', function (req, res, next) {
MailSender.getPending();
});
And mailer file:
(here include all required)
module.exports = {
currentMails : {},
getPending : function() {
MailObj.find()
.limit(10)
.exec(this.blockPending);
},
blockPending : function(err, mail) {
currentMails = {};
mail.forEach(function(data) {
let mailId = mongoose.Types.ObjectId(data._id);
currentMails[mailId] = data;
});
MailObj.update({ _id: { $in: Object.keys(currentMails) } }, { block: 1 }, {multi: true}, function() {
// Doesn't work
this.myNextFunc();
});
},
myNextFunc : function() {
console.log("Yep!");
}
}
getPending - it works great and call blackPending with query results.
blockPending - works greats, I can prepare ids and update records
But... myNextFunc() doesn't work and I can't call any object function from this scope (console says that they are undefined). I know, that I make something wrong but... what?
I'ld like to encapsule related functions in such objects and run inside as callbacks. What am I doing wrong?
As far as you are using Mongoose, why don't you take profit of it, and you update each mail in the loop?? It is less efficient, but maybe as a first approach it deserves:
var numUpdates = 0;
mail.forEach(function(data) {
data.block = 1;
data.save(function(err, mailSaved) {
//check error
if(numUpdates ++ >= mail.length) {
this.myNextFunc();
}
})
});
Related
I'm new to node.js and currently working on a project using keystonejs cms and MongoDB. Now I'm stuck in getting data related to multiple collections. Because of this callback functions, I couldn't return an array with relational data. My code something similar to this sample code.
var getAgenda = function(id, callback){
callback = callback || function(){};
if(id){
AgendaDay.model.find({summit:id}).exec(function (err, results3) {
var arr_agenda = [];
var arr_agenda_item = [];
for(var key3 in results3){
AgendaItem.model.find({agendaDay:results3[key3]._id}).exec(function (err, results2){
for(var key2 in results2){
arr_agenda_item.push(
{
item_id: results2[key2]._id,
item_name: results2[key2].name,
from_time: results2[key2].time_from,
to_time: results2[key2].time_to,
desc: results2[key2].description,
fatured: results2[key2].featured,
}
);
}
arr_agenda.push(
{
name: results3[key3].name,
date: results3[key3].date,
description: results3[key3].description,
item_list:arr_agenda_item
}
);
return callback(arr_agenda);
});
}
});
}
}
exports.list = function (req, res) {
var mainarray = [];
Summit.model.find().exec(function (err, resultssummit) {
if (err) return res.json({ err: err });
if (!resultssummit) return res.json('not found');
Guest.model.find().exec(function (err, resultsguset) {
for(var key in resultssummit){
var agen_arr = [];
for(var i=0; i<resultssummit[key].guests.length; i++){
var sumid = resultssummit[key]._id;
//this is the function im trying get data and assign to mainarray
getAgenda(sumid, function(arr_agenda){
agen_arr = arr_agenda;
});
mainarray.push(
{
id: resultssummit[key]._id,
name: resultssummit[key].name,
agenda_data: agen_arr,
}
);
}
res.json({
summit: mainarray,
});
}
});
}
}
If anyone can help me out, that would be really great :)
You need to restructure this whole thing. You should not be calling mongo queries in a for loop and expecting their output at the end of the loop. Also, your response is in a for loop. That won't work.
I'll tell you how to do it. I cannot refactor all of that code for you.
Instead of putting mongodb queries in a for loop, you need to convert it in a single query. Just put the _ids in a single array and fire a single query.
AgendaItem.model.find({agendaDay:{$in:ARRAY_OF_IDS}})
You need to do the same thing for AgendaDay.model.find({summit:id}) as well.
I want to use an module to get and process data from my MongoDB database. (It should generate an object that represents my Express.js site's navbar)
I thought of doing something like this:
var nav = { Home: "/" };
module.exports = function() {
MongoClient.connect(process.env.MONGO_URL, function(err, db) {
assert.equal(err, null);
fetchData(db, function(articles, categories) {
combine(articles, categories, function(sitemap) {
// I got the data. What now?
console.log("NAV: ", nav);
})
});
});
};
var fetchData = function(db, callback) {
db.collection('articles').find({}).toArray(function(err, result) {
assert.equal(err);
articles = result;
db.collection('categories').find({}).toArray(function(err, result) {
assert.equal(err);
categories = result;
db.close();
callback(articles, categories);
});
});
};
var combine = function(articles, categories, callback) {
categories.forEach(function(category) {
nav[category.title] = {};
articles.forEach(function(article) {
if(article.category == category.name) {
nav[category.title][article.title] = "link";
}
})
});
callback(nav);
};
As of line 6, I do have all data I need.
(An object, currenty like { Home: '/', Uncategorized: { 'Hello world!': 'link' } })
But since I'm in an anonymous function, I don't know how to return that value. I mean, return would just return it the function that called it... And in the end, MongoClient.connect would receive my data.
If I set a variable instead, it would be set as module.exports returned before Node can even query the data from the database, right?
What can I do in order to make this work?
It should result in some kind of function, like
var nav = require('nav');
console.log(nav());
Thanks in advance!
Add another callback:
var nav = { Home: "/" };
module.exports = function(cb) {
MongoClient.connect(process.env.MONGO_URL, function(err, db) {
assert.equal(err, null);
fetchData(db, function(articles, categories) {
combine(articles, categories, function(sitemap) {
cb(sitemap);
})
});
})
});
And then use this way:
var nav = require('nav');
nav(function(sitemap){ console.log(sitemap); });
You can use mongoose module or monk module. These modules have been tested properly .
Just use
npm install mongoose or monk
The suggestion about mongoose is great and you can look into it, however I think you've already done the job with the fetching of the data from the db. You just need to access it in your main node flow.
You can try this:
module.exports.generateNav = function() {
MongoClient.connect(process.env.MONGO_URL, function(err, db) {
assert.equal(err, null);
var output = fetchData(db, function(articles, categories) {
combine(articles, categories, function(sitemap) {
})
});
return (output);
});
};
And then in your main application you can call it in the following way:
var nav = require('nav');
navigation = nav.generateNav();
console.log(navigation);
what's the proper way to check for undefined values? What I want to do is to have a PUT method that will update those fields that are not empty. For example, if I send req.body.name = 'John' and no req.body.job I want my request to only change the name.
Some code:
router.put('/:id', (req, res) => {
const query = {_id: req.params.id};
const update = {
$set: {
name: req.body.name,
job: req.body.job
}
};
User.findOneAndUpdate(query, update,
(err, userUpdated) => {
if (err) {
console.log('Error while updating');
console.log(err);
} else {
res.send(userUpdated);
}
});
});
This will of course throw an error:
CastError: Cast to number failed for value "undefined" at path "job"
Now I can manually check if req.body.job is empty and if it is set it's value to the value the user had previously, but that seems like a hack, not elegant and a lot of writing for each route.
I have checked the docs but none of the options provided there seem to do the job. I also came across something like express validator but this will probably just do a return if the value is empty. Another options would be to simply send the value from the front-end part.
I'm new to backend development and I'm not sure if I'm doing stuff the "right way". So please, any comment on how it should be done would be nice (also if my code looks odd, feel free to guide me :)), thanks!
You can write your own method to do this.
For example this example
var req = {body: {name: undefined, job: 'yes'}};
const _ = require('lodash');
const out = {};
_(req.body).forEach((value,key) => {
if (!_.isEmpty(value)){
out[key] = value;
}
});
console.log(out);
Is having this output
{ job: 'yes' }
You can also write it as middleware, if you want, if you write it as this
function onlyNotEmpty(req, res, next) => {
const out = {};
_(req.body).forEach((value, key) => {
if (!_.isEmpty(value)) {
out[key] = value;
}
});
req.bodyNotEmpty = out;
next();
}
Then you can write your method with middleware
router.put('/:id', onlyNotEmpty, (req, res) => {
const query = {_id: req.params.id};
const update = {
$set: req.bodyNotEmpty
};
// This will be the same
});
I am pretty new to Node.js or Javascript in general when it comes to serverside stuff. Currently I am tring to validate some of the user input and set default values if something is wrong. Now if I run my validation the json object appears in the database befor my validation is completed.
The way I am doing the validation isnt maybe the best right now but if someone can explain me the behavior, I am pretty sure i can understand Javascript alot better in the future.
Is there a better way of doing validation (without mongoose or other ODM modules) with callbacks, middleware or should I use some async module?
Here is my code:
module.exports = function(app, express, todoDB, listDB, statusDB) {
var moment = require('moment');
var todoRouter = express.Router();
todoRouter.post('/', function(req, res, next) {
console.log('1');
if (!(moment(req.body.createDate).isValid())) {
req.body.createDate = moment().format("DD-MM-YYYY HH:mm:ss");
}
else {
req.body.createDate = moment(req.body.createDate).format("DD-MM-YYYY HH:mm:ss");
}
console.log('2');
if (req.body.list_id == '') {
listDB.findOne({list: 'Neu'}, function(reqdb, docs) {
if (docs == null) {
listDB.insert({list: 'Neu', index: 1});
listDB.findOne({list: 'Neu'}, function(reqdb, docs) {
console.log('AnlageListID');
console.log(docs._id);
req.body.list_id = docs._id;
});
}
else {
console.log('BestehendeListID');
console.log(docs._id);
req.body.list_id = docs._id;
}
});
}
console.log('3');
if (req.body.status_id == '') {
statusDB.findOne({status: 'offen'}, function(reqdb, docs) {
if (docs == null) {
statusDB.insert({status: 'offen', index: 1});
statusDB.findOne({status: 'offen'}, function(reqdb, docs) {
console.log('AnlageStatusID');
console.log(docs._id);
req.body.status_id = docs._id;
});
}
else {
console.log('BestehendeStatusID');
console.log(docs._id)
req.body.status_id = docs._id;
}
});
}
console.log('4');
console.log('StatusID');
console.log(req.body.status_id);
console.log('ListID');
console.log(req.body.list_id);
todoDB.insert({
todo: req.body.todo,
createDate: req.body.createDate,
endDate: req.body.endDate,
discription: req.body.discription,
comment: req.body.comment,
list_id: req.body.list_id,
priority_id: req.body.priority_id,
section_id: req.body.section_id,
user_id: req.body.user_id,
status_id: req.body.status_id,
company_id: req.body.company_id
});
res.json({message: 'TODO erfolgreich hinzugefĆ¼gt!'});
});
return todoRouter;
};
... and this is the ouput:
1
2
3
4
StatusID
ListID
POST /api/todos 200 76.136 ms - 44
BestehendeListID
M3Xh46VjVjaTFoCM
BestehendeStatusID
48v80B4fbO87c8um
PS: Its a small "project" just for me learing the MEAN Stack so I am using neDB.
If I understand correctly you try to sequentially execute a number of asynchronous calls and introduce checks in the code to validate if previous asynchronous calls have completed. This is not going to work in a general case because your checks may be processed before the asynchronous call goes through. It might work now and then just by chance, but I would not expect even that.
There are standard mechanisms for that. One of them is using promises, another one using async and yet another one if stacking up all callbacks one into another. Below I will demonstrate how to address the problem using async, but the same general idea applies to using promises. Check the async project on Github then the following part-solution will become clear:
var async = require("async")
async.waterfall([
function(next) {
listDB.findOne({list: 'Neu'}, next); // quits on error
},
function(doc, next) {
if (doc) {
return next(null, doc._id);
}
statusDB.insert({status: 'offen', index: 1}, function(err) {
if (err) return next(err); // quit on error
statusDB.findOne({status: 'offen'}, function(err, doc) {
next(err, doc._id); // quits on error
});
});
},
function(id, next) {
// do next step and so on
next();
}
],
// this is the exit function: it will get called whenever an error
// is passed to any of the `next` callbacks or when the last
// function in the waterfall series calls its `next` callback (with
// or without an error)
function(err) {
console.error("Error processing:", err)
});
I use nodejs and express for my js server.
I need a way to fetch the number of models inside a collection and send it to Backbone.
The express side is rather straight forward:
db.books.count(function (err, booksNr) {
if (err) return;
console.log(booksNr)
res.json(booksNr);
});
But how do i send this over to backbone?
My first thought was to bing the booksNr to a collection, but I can't make it work.
Here is how I fetch a collection from Backbone:
books.fetch(
{
data: {
'fetchType' : 'list',
'currentPage': 1,
'perPage': 3
},
success: function(books) {
console.log(books);
var books = books.models;
var template = _.template(listBooksTpl, {
books : books,
});
setTimeout(function () {
that.$el.html(template);
$(that.preLoader).hide();
that.$el.show('slow');
}, 500);
},
error: function() {
console.log("BookList error!");
}
}
);
Here is how I send the collection data to Backbone:
exports.books.paginated = function (req, res) {
var currentPage = parseInt(req.query.currentPage - 1),
perPage = parseInt(req.query.perPage),
skip = parseInt(currentPage * perPage);
db.books.find({}).limit(perPage).skip(skip, function (err, books) {
if (err) return;
res.send(books);
});
};
Any ideas?
It is quite simple, you need an api for retrieving books, in express.js:
app.get('/api/books', function(req, res){
var query = createQueryFromReq(req);
BookModel.find(query).exec(function(err, books) {
res.send(books);
});
});
Then you need a Books Backbone.Collection in your client side, notice that it does the parsing automatically, you don't need to add your own success parser:
var Book = Backbone.Model.extend({});
var Books = Backbone.Collection.extend({
model:Book,
url: "/api/books",
});
// Some main.js
var myBooks = new Books();
myBooks.fetch({
data: {/* your data */},
reset: true // Trigger reset in backbone 1.0+
});
Now, you should listen to "reset" event fired on myBooks and act upon it
After Comments:
The need is to be able to paginate from the server, I would recommend using https://github.com/backbone-paginator/backbone.paginator
if you want to use your own solution, you could be using this approach, on the Server side supply the extra information:
app.get('/api/books', function(req, res){
var limit = req.params.limit;
var offset = req.params.offset;
var bookCount = getBookCount(); // how many books we have
BookModel.find(query).skip(offset).limit(limit).exec(function(err, books) {
res.send({
limit: limit,
offset: offset,
total: bookCount,
books:books,
});
});
});
On the Client side you should use the parse function on Books:
var Books = Backbone.Collection.extend({
model:Book,
url: "/api/books",
parse: function(response){
this.total = response.total;
this.offset = this.offset + this.books.length; // I would test this line :) it might need a -1 also here
return response.books; // only using the books part of the response to create the models
}
});
Now you need to keep track on your limit and offset, and in the first time you fetch use data give it offset zero:
myBooks.fetch({
data: {offset:0, limit:10},
});
Another alternative it to have this Collection part of a Paginator Model, (but you can just use backbone.paginator for this)
Upon more research, here is how I did it...
Is this a good way to go?
Is it a good practice to fet a model without an ID?
In Backbone, I instantiate a model without any id:
// Random model - get total number of books.
book = new Book();
// Get the total number of books.
book.fetch({
data : { 'fetchType' : 'booksNr' },
success : function (booksNr) {
that.booksNr = booksNr.get('booksNr');
},
error : function () {
console.log("Error! Failed to get the total number of books.");
}
});
In express, I look for the query string: fetchType and do the following:
exports.books.paginated = function (req, res) {
var fetchType = req.query.fetchType;
if (fetchType == 'booksNr') {
db.books.count(function (err, booksNr) {
if (err) return;
res.json({ 'booksNr' : booksNr });
});
}
};