Filtering MongoDB data in controller - javascript

I have this snippet of code in my controller but I'd like to filter the incoming records by a field archived == true in the MongoDB document.
Here is the filterless code that works right now. I'm not sure where to add the filter. I tried adding a simple filter() using prototype but it broke. What am I missing?
var Candidate = require('../models/candidate');
var async = require('async');
// Display list of all Candidate
exports.candidate_list = function(req, res, next) {
Candidate.find({}) //should something go in between the {}?
.sort([['name', 'ascending']])
.exec(function (err, list_candidates) {
if (err) { return next(err); }
//Successful, so render
res.render('candidate_list', { title: 'Candidates', list_candidates: list_candidates});
});
};

You can try this
var Candidate = require('../models/candidate');
var async = require('async');
// Display list of all Candidate
exports.candidate_list = function(req, res, next) {
Candidate.find({achived:true})
.sort([['name', 'ascending']])
.exec(function (err, list_candidates) {
if (err) { return next(err); }
//Successful, so render
res.render('candidate_list', { title: 'Candidates', list_candidates: list_candidates});
});
};

Related

Send a JSON array as response from Node.js

I am fetching data from a MongoDB database then putting it in a cursor to send that as a Node.js response.
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost/EmployeeDB';
/* GET users listing. */
router.get('/', function(req, res, next) {
//res.send('respond with a resource');
MongoClient.connect(url, function(err, db) {
var cursor = db.collection('Employee').find();
cursor.each(function(err, doc) {
console.log(doc);
arrayres = doc ;
res.send(doc);
});
db.close();
});
});
module.exports = router;
It sends only the first record then I get this error:
Error [ERR_HTTP_HEADERS_SENT]: Cannot remove headers after they are sent to the client
at ServerResponse.removeHeader (_http_outgoing.js:528:11)
at ServerResponse.send
Notice: I get this error only when there are multiple records to send as response.
You are sending the response twice. Which is impossible ( look at Why can't we do multiple response.send in Express.js? )
res.send('respond with a resource');
Here and
res.send(arrayres);
Here.
Here is a working example based on jeremy's answer :
router.get('/', function (req, res, next) {
MongoClient.connect(url, function (err, db) {
var cursor = db.collection('Employee').find();
let employees = []
const pushData = async () => {
cursor.forEeach( function (doc) {
employees.push(doc);
});
}
const sendResponse = async () => {
await pushData();
res.json(employees);
}
});
});
You can only send back one response to the browser (be it res.send(), res.end(), res.sendFile(), res.json() or any other). So, you can't have that inside a .forEach().
First, build an array, then send your data back once.
router.get('/', function (req, res, next) {
MongoClient.connect(url, function (err, db) {
var cursor = db.collection('Employee').find();
let employees = []
cursor.forEeach( function (doc) {
employees.push(doc);
});
res.json(employees);
});
});
Or with Mongoose :
Employee.find().lean().exec( (err,docs) => res.json(docs))

How do I use a callback to fetch data from mysql with node.js and ejs?

I need to fetch foo from the query below:
exports.get = function(id, cb) {
sql = 'SELECT `sidebar`, `test` FROM users WHERE `id` = "' + id + '"';
con.query(sql, function(err, foo) {
if (err) { return cb(err) }
else { return cb(foo) };
});
}
Then render foo on my app.js like this:
app.get('/dashboard', ensureLoggedIn('/login'),
function(req, res) {
const id = req.session.passport.user;
const foo = db.defaults.set(id) //db.defaults.set calls the query
console.log(foo); //prints undefined
res.render('dashboard', { foo:foo });
});
This was my latest attempt:
app.get('/dashboard', ensureLoggedIn('/login'),
function(req, res, next) {
const id = req.session.passport.user;
db.defaults.get(id, function(err, foo) {
if (err) return next(err);
res.render('dashboard', {foo:foo});
//also tried:
//return res.render('dashboard', {foo:foo});
});
The attempt above doesn't render the page at all. The html loads as:
[object object]
What am I missing?
You're trying to render server side a template asynchronously (after fetching the data) and you can't do that.
What you can do is send synchronously the template to the client and then expose an endpoint to fetch those data and modify the page accordingly (on the client).
app.get('/dashboard', ensureLoggedIn('/login'), function(req, res) {
res.render('dashboard');
});
app.get('/fetchdata', function (req, res, next) {
const id = req.session.passport.user;
db.defaults.get(id, function(err, foo) {
if (err) return next(err);
res.send(foo);
});
})

Updating model with Sequelize JS

I'm building a simple API with Express and Sequelize JS. I tried to update an record with req.School but nothing happend. I shared the code below. Also I got no error for output. Can you help me ?
Controller js
module.exports.params = function(req, res, next, id) {
School.find({
where: {
id: id
}
})
.then(function(school) {
if (!school) {
next(new Error("School not found by given ID"));
}else {
req.School = school;
next();
}
}, function(err) {
next(err);
});
};
module.exports.delete = function (req,res,next) {
req.School.Status = SysEnums.Deleted;
School.update(req.School,{
where:{
id:req.School.id
}
}).then(function (deleted) {
res.json(deleted);
});
};
Route JS
var router = require("express").Router();
var controller = require("../Controllers/SchoolController");
router.param("id",controller.params);
router.route("/").get(controller.get);
router.route("/:id").get(controller.getOne).delete(controller.delete);
module.exports = router;
Since the params method, req.School is a sequelize object. Here is the params method.
module.exports.params = function(req, res, next, id) {
School.find({
where: {
id: id
}
})
.then(function(school) {
if (!school) {
next(new Error("School not found by given ID"));
}else {
req.School = school;
next();
}
}, function(err) {
next(err);
});
};
As #Shivam said it' s a sequelize object, I could use provided methods. I want to soft delete my record, so here is my delete method with Sequelize JS.
module.exports.delete = function(req, res, next) {
try {
req.School.Status = SysEnums.Deleted;
req.School.save().then(function(entity) {
res.json(entity);
});
} catch (e) {
console.log(e);
}
};
#PandhiBhaumik
Thank you for your help. I' ll try to use build method as a second option.

Send 2 array to view in sailjs

I am new to sails.js and when I'm trying to learn it by making a simple web app, I encountered a problem. My app is about class management. My model has 3 table: student, course and list. The student table save information about students, course save information about courses. The list.table save information about which student is in a course and their mark, their absent days in this class. I used res.view() and it worked for any view that need one array. However, when I try to write function which need information from 2 model: student and course, res.view() didnt help (or I dont know to do it properly)
module.exports = {
'new': function(req, res, next) {
Course.find(function foundCourses (err, courses) {
if (err) return next(err);
res.view({courses: courses});
});
/* Student.find(function foundStudents (err, students) {
if (err) return next(err);
res.view({
students: students
});
}); */
},
//more other code here
};
There, I want to send both array to view but I dont know how. Sails.js only let me send one array if I do like that. Please help me . Thank you a lot!
You can't call res.view twice. You need to gather all data first and than send it to view.
2 methods:
module.exports = {
'new': function(req, res, next) {
var foundCourses = function (err, courses) {
if (err) return next(err);
Student.find(function foundStudents (err, students) {
if (err) return next(err);
res.view({
courses: courses,
students: students
});
});
});
Course.find(foundCourses);
},
//more other code here
};
or you can use async.parallel
module.exports = {
'new': function(req, res, next) {
var dataCallback = function (err, data) {
if (err) return next(err);
var courses = data[0];
var students = data[1];
res.view({
courses: courses,
students: students
});
};
async.parallel([
function(callback) {
Course.find(callback);
},
function(callback) {
Student.find(callback);
}
],dataCallback);
},
};

Remove a field of my json in a specific case

I have an ExpressJS controller that list all my users
userCtrl.get :
get(req, res, next) {
var func = function(err, data) {
if (err) return next(err);
return res.json(data);
};
if (req.params[this.idName])
this._getById(req.params[this.idName], func);
else
this._getAll(func);
}
_getById(id, fn) {
this.ObjectClass.findById(id, fn);
}
_getAll(fn) {
this.ObjectClass.findAll(fn);
}
I'd like to call it from another road, in such a way that res.json() will filter a field of this json
Something like :
router.get ('/services/:serviceKey/authBridge/users', function(req, res, next) {
function anonJs(x) {
x.forEach(s => s.credential = null);
res.json(x);
}
res.json = anonJs;
userCtrl.get(req, res, next);
});
The problem is, with this last piece of code I end up with a recursion as I call res.json that is now defined as anonJS
You must store the reference to the old function before replacing it.
router.get ('/services/:serviceKey/authBridge/users', function(req, res, next) {
var json = res.json;
res.json = function(x) {
x.forEach(s => s.credential = null);
json(x);
}
userCtrl.get(req, res, next);
});

Categories