Reputation: 153
I have a function that checks whether the first name, last name and email of a new user is the same as what's in my node.js mock database (JSON file). It's failing because the email is undefined
after the first iteration. i.e. If I add a user with duplicate email it will fail because the comparison db_email === email
would never be true (after the first iteration).
What is happening? Am I missing something glaringly obvious?
db.json
[{"basic_info":{"first_name":"Diego","last_name":"Gonzalez","phone_number":"451-124-5112","postal_code":"G1F 1F5"},"address":{"email_address":"[email protected]","street_no_and_name":"45 Kirklane Drive","city":"Rome","country":"Italy"},"banking_info":{"occupation":"accountant","annual_salary":45000,"monthly_expenses":2000},"products":{"chequing":true,"savings":true,"line_of_credit":false,"mortgage":true,"credit_card":true},"user_id":1},{"basic_info":{"first_name":"Marie","last_name":"Spitzer","phone_number":"640-998-3252","email_address":"[email protected]"},"address":{"street_no_and_name":"425 Godstone Lane","city":"Mexico City","country":"Mexico","postal_code":"Q1P 0F1"},"banking_info":{"occupation":"firefighter","annual_salary":95000,"monthly_expenses":5000},"products":{"chequing":false,"savings":false,"line_of_credit":false,"mortgage":false,"credit_card":true},"user_id":2},{"basic_info":{"first_name":"Jacob","last_name":"Karr","phone_number":"437-990-2275","email_address":"[email protected]"},"address":{"street_no_and_name":"23","city":"Rowena Drive","country":"Timbucktoo","postal_code":"M5H3P5"},"banking_info":{"occupation":"Consultant","annual_salary":"900001","monthly_expenses":"2440"},"products":{"Chequing":false,"Savings":false,"Line of Credit":false,"Mortage":false,"Credit Card":true},"user_id":3},{"basic_info":{"first_name":"Jacob","last_name":"Karr","phone_number":"437-990-2275","email_address":"[email protected]"},"address":{"street_no_and_name":"23","city":"Rowena Drive","country":"Timbucktoo","postal_code":"M5H3P5"},"banking_info":{"occupation":"Consultant","annual_salary":"900001","monthly_expenses":"2440"},"products":{"Chequing":false,"Savings":false,"Line of Credit":false,"Mortage":false,"Credit Card":true},"user_id":4}]
api.js
const validateRegistration = (fn, ln, email) => {
const reg = fs.readFileSync('db/registration.json', 'utf8');
const regObject = JSON.parse(reg);
const existingUsers = regObject.filter(userInfo => {
const db_fn = userInfo["basic_info"]["first_name"];
const db_ln = userInfo["basic_info"]["last_name"];
const db_email = userInfo["address"]["email_address"];
console.log(`${db_fn} === ${fn} = ${db_fn === fn}`);
console.log(`${db_ln} === ${ln} = ${db_ln === ln}`);
console.log(`${db_email} === ${email} = ${db_email === email}`);
if (db_fn === fn && db_ln === ln && db_email === email) { return true; }
})
return existingUsers.length > 0 ? true : false;
}
console.log output
Diego === Jacob = false
Gonzalez === Karr = false
[email protected] === [email protected] = false
Marie === Jacob = false
Spitzer === Karr = false
undefined === [email protected] = false
Jacob === Jacob = true
Karr === Karr = true
undefined === [email protected] = false
Thanks in advance for the help!
Upvotes: 1
Views: 55
Reputation: 153
Figured it out; also feel quite stupid.
I placed 'email_address' under 'address' in the JSON where it should have been under 'basic_info'. Also should have been accessing it via const db_email = userInfo["basic_info"]["email_address"];
in api.js
Upvotes: 3
Reputation: 933
{
"basic_info": {
"first_name": "Jacob",
"last_name": "Karr",
"phone_number": "437-990-2275",
"email_address": "[email protected]"
},
"address": {
"street_no_and_name": "23",
"city": "Rowena Drive",
"country": "Timbucktoo",
"postal_code": "M5H3P5"
},
...
"user_id": 4
}
Looks like email_address
is actually nested under basic_info
for all your users except the first.
Upvotes: 0
Reputation: 286
This is expected javascript behavior: return undefined when a reference is not set.
You're JSON array does not necessarily have an 'email_address' field for every object, therefore 'undefined' is the value placeholder since it is not found.
Example of your user object[address] without an email_address:
"address":{"street_no_and_name":"23","city":"Rowena Drive","country":"Timbucktoo","postal_code":"M5H3P5"}
Upvotes: 2