Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

Commit

Permalink
behave better when req.body has falsey values
Browse files Browse the repository at this point in the history
  • Loading branch information
http://jneen.net/ committed Dec 2, 2019
1 parent d6765e2 commit 7d7ca1e
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions utils/route.helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,15 @@ class Route {
* @param {*} defaults object in the format of keyname: 'defaultValue', 'surname': 'Boisvert' (if you wanted a default surname of Boisvert...)
*/
loadKeys(keys, defaults = {}) {
const has = (o, k) => o && Object.prototype.hasOwnProperty.call(o, k)

return (req, res, next) => {
// copy data from these sources in order, falling through
// if each is not present.
keys.forEach(k => {
res.locals[k] =
(req.body || {})[k]
|| (req.session || {})[k]
|| defaults[k]
res.locals[k] = has(req.body, k) ? req.body[k]
: has(req.session, k) ? req.session[k]
: defaults[k]
})

// make all variables available on a global `data` field, to
Expand Down

0 comments on commit 7d7ca1e

Please sign in to comment.