Remove a field of my json in a specific case - javascript

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

Related

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

Passport Js documentation Custom Callback syntax

I am using passport.js for my node app. In the Custom Callback section I found the following code(lets say it Code A):
app.get('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/users/' + user.username);
});
})(req, res, next);
});
My question is I am not able to understand passing (req, res, next) at the end. How the following code(lets say it Code B):
app.get('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/users/' + user.username);
});
});
});
How Code B is different from Code A ?
If I simplify Code A then it will be like :
app.get('/login', function(req, res, next) {
passport.authenticate('local', function(..){..})(req, res, next);
});
Further
passport.authenticate(..)(req, res, next);
which means expression like
function(..)(req,res, next)
My Question is more about understanding the syntax
function(..)(parameters)
authenticate() function is structured like this:
module.exports = function authenticate(passport, name, options, callback) {
// ...
return function authenticate(req, res, next) {
// ...
strategy.success = function(user, info) {
if (callback) {
return callback(null, user, info);
}
}
// ...
})
};
So it takes two series of parameters:
The first one (in your case 'local' and the callback function) is used to tell passport to authenticate you, and how to do it;
The second handles the function to control your app with req, res and next parameters.
Hope it helps you to understand!

Pass values from referenced function in promise-chain?

I'm writing some rendering-code for an Express app, I wish to catch errors and then output them in the function render, but I'm not sure how I'm going to move them from one method to the other.
app.get('/user/makeRider', auth,
(req, res, next) => {
req.user.user.makeRider(req.query)
.catch(error)
.then(render(req, res));
}
);
var render = (req, res) => {
var response = {
params: req.query,
user: req.user.fulluser
};
res.json(response);
},
error = (reason) => {
reason.errors.forEach((error) =>{
console.log(error);
});
return;
};
You can use error function as your last midleware in the chain and simply pass the request to the next chain:
var render = (req, res) => {
var response = {
params: req.query,
user: req.user.fulluser
};
res.json(response);
}
app.get('/user/makeRider', auth,
(req, res, next) => {
req.user.user.makeRider(req.query)
.catch(next)
.then(render(req, res));
}
);
app.use((reason, req, res, next) => {
res.send(reason.errors);
// or you can send them as json: res.status(404).json({errors: reason.errors})
});
Beware of hoisting issue in your code, the variable declarations are hoisted to the top, but not their assignments, error and render function may appear as undefined when accessed from your route.
A quick, but maybe not the most elegant solution would be to add errors as a parameter to your render function, then you could do something like this:
app.get('/user/makeRider', auth,
(req, res, next) => {
req.user.user.makeRider(req.query)
.catch((reason)=>{
render(req, res, reason.errors)
})
.then(render(req, res));
}
);
var render = (req, res, errs) => {
var response = {
params: req.query,
user: req.user.fulluser
};
res.json(response);
};

How to call an express export method internally?

This is my first method that responds well to an http call:
exports.create = function(req, res, next){
var submission = new task(req.body);
submission.save(function(err){
if(err){
return next(err);
} else {
res.json(submission);
}
});
};
Now, I want to call this from a method defined in the same file:
exports.evaluate = function(req, res, next){
if(req.body.value == x){
// need to call the above method
this.create(req.body.someobject)
}
};
How can this be done?
The right way to do this is to define a common method which isn't a route handler and call it from the handlers.
var _create = function (object, callback) {
var submission = new task (object);
submission.save(callback);
}
exports.create = function(req, res, next){
_create(req.body, function(err){
if(err){
return next(err);
} else {
res.json(submission);
}
});
};
exports.evaluate = function(req, res, next){
if(req.body.value == x){
// need to call the above method
_create(req.body.someobject, function (err) {
// Send response
});
}
};

Node.js async consistency

I have the following code :
server.use(function(req, res, next) {
users_db.set(req.user, function(err) { // async call to mongodb
if (err) {
console.error(err);
}
});
}
return next();
});
server.get('/', function(req, res) {
req.user.active = true; // this is a new field in user object
res.send(req.user);
}
});
So, As you see, when users_db.set() is called, req.user doesn't have the active=true field. It is being inserted only in the server.get() function.
Is it possible that user.active = true is registered in the db nevertheless because of the asynchronous nature of the call ?
As far as I know (it is like that in Express at least) .get method accepts many middleware functions. So I guess that the following will work:
server.get(
'/',
function(req, res, next) {
req.user.active = true; // this is a new field in user object
res.send(req.user);
next();
},
function(req, res, next) {
users_db.set(req.user, function(err) { // async call to mongodb
if (err) {
console.error(err);
}
});
}
return next();
}
);
Doing the things like that you are sure that req.user.active is populated always before to reach the moment with users_db.set.

Categories