[英]Getting undefined when iterating through a parsed JSON
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). 我有一个检查新用户的名字,姓氏和电子邮件是否与我的node.js模拟数据库(JSON文件)中的相同的函数。 It's failing because the email is undefined
after the first iteration. 失败是因为第一次迭代后undefined
电子邮件。 ie 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). 即,如果我添加具有重复电子邮件的用户,它将失败,因为比较db_email === email
永远不会为真(在第一次迭代之后)。
What is happening? 怎么了? Am I missing something glaringly obvious? 我是否缺少明显的东西?
db.json db.json
[{"basic_info":{"first_name":"Diego","last_name":"Gonzalez","phone_number":"451-124-5112","postal_code":"G1F 1F5"},"address":{"email_address":"dgonz@gmail.com","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":"mspitzer@gmail.com"},"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":"jacob.carr@carr.com"},"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":"jacob.carr@carr.com"},"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 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 console.log输出
Diego === Jacob = false
Gonzalez === Karr = false
dgonz@gmail.com === jacob.carr@carr.com = false
Marie === Jacob = false
Spitzer === Karr = false
undefined === jacob.carr@carr.com = false
Jacob === Jacob = true
Karr === Karr = true
undefined === jacob.carr@carr.com = false
Thanks in advance for the help! 先谢谢您的帮助!
Figured it out; 弄清楚了; also feel quite stupid. 也觉得很傻。
I placed 'email_address' under 'address' in the JSON where it should have been under 'basic_info'. 我将“ email_address”放在JSON的“地址”下,应该放在“ basic_info”下。 Also should have been accessing it via const db_email = userInfo["basic_info"]["email_address"];
还应该通过const db_email = userInfo["basic_info"]["email_address"];
来访问它const db_email = userInfo["basic_info"]["email_address"];
in api.js 在api.js中
This is expected javascript behavior: return undefined when a reference is not set. 这是预期的javascript行为:未设置引用时返回undefined。
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. 您是JSON数组,不一定每个对象都有一个'email_address'字段,因此'undefined'是值占位符,因为未找到它。
Example of your user object[address] without an email_address: 没有电子邮件地址的用户对象[地址]的示例:
"address":{"street_no_and_name":"23","city":"Rowena Drive","country":"Timbucktoo","postal_code":"M5H3P5"}
{
"basic_info": {
"first_name": "Jacob",
"last_name": "Karr",
"phone_number": "437-990-2275",
"email_address": "jacob.carr@carr.com"
},
"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. 看来,除第一个用户外,所有用户的email_address
实际上都嵌套在basic_info
下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.