Skip to content
This repository has been archived by the owner on Mar 7, 2019. It is now read-only.

Hotfix registration to add sane default name #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ router.get('/logout',
// register new user with local strategy
router.post('/register', (req, res) => {
Student.register(new Student({ username: req.body.username }),
req.body.password, (regErr, account) => {
req.body.password, (regErr, newAccount) => {
if (regErr) {
console.error(regErr);
}
// hotfix. Redux demands these fields be filled,
// but they have no default values
const account = Object.assign(newAccount, {
name: 'Test User',
entryYear: '2001',
major: 'Mechanical Engineering',
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code above actually overwrites everything in account and newAccount with name: 'Test User' etc., even if newAccount.name has a value. Not sure if that's what you wanted.

To merge the objects with precedence to supplied newAccount properties, use:
const account = Object.assign({name: 'Test User', entryYear: '2001', major: 'Mechanical Engineering'}, newAccount) (if you keep the defaults as an object literal – in that case, the literal is mutated into the merged object each time)
or
const account = Object.assign({}, AccountDefaults, newAccount) (if you move the defaults to a global).

If you're using redux, you've probably got Babel / webpack set up with the object rest spread operator. In that case, you can also write
const account = {...AccountDefaults, ...newAccount}

https://jsfiddle.net/owsteele/5wk3tzLf/1/

account.save((saveErr) => {
if (saveErr) {
console.error(saveErr);
Expand Down