遍历解析的JSON

时间:2019-03-25 20:00:16

标签: javascript

我有一个函数可以检查新用户的名字,姓氏和电子邮件是否与我的node.js模拟数据库(JSON文件)中的相同。失败是因为第一次迭代后电子邮件为undefined。即,如果我向用户添加重复的电子邮件,则它将失败,因为比较db_email === email永远不会为真(在第一次迭代之后)。

发生了什么事?我是否缺少明显的东西?

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

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输出

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

预先感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

弄清楚了;也觉得很傻。

我在JSON的“地址”下放置了“电子邮件地址”,它应该在“ basic_info”下。还应该已经通过api.js中的const db_email = userInfo["basic_info"]["email_address"];访问它

答案 1 :(得分:2)

这是预期的javascript行为:未设置引用时返回未定义。

您是JSON数组,不一定对每个对象都有一个'email_address'字段,因此'undefined'是值占位符,因为找不到它。

没有电子邮件地址的用户对象[地址]的示例:

onCheck(id) { this.setState({ checked: !this.state.checked }); }

答案 2 :(得分:0)

  {
    "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
  }

对于您的所有用户,除第一个用户外,看起来email_address实际上嵌套在basic_info下。