【问题标题】:How to find array elements in npm?如何在 npm 中查找数组元素?
【发布时间】:2021-12-31 22:21:28
【问题描述】:

我试图从这个用户虚拟数组中找到输入电子邮件和密码的用户,但我在返回值中未定义,因此我无法登录。如果我在控制台中运行此代码,它会获取正确的输出。有人能帮忙解释一下为什么这个 find 函数(在 post req 中)在 Node 中不起作用吗?

虚拟数组-

const users = [
            { id: 1, name: 'Sherry', email:'sherry@gmail.com', password: 1234 },   
            { id: 2, name: 'harry', email:'harry@gmail.com', password: 1234 },
            { id: 3, name: 'cherry', email:'cherry@gmail.com', password: 1234 }
        ]

获取请求 -

app.get('/login', (req, res) => {
        res.send(`<form method='POST' action='/login'>
            <input type="email" name="email" placeholder="Enter email" />
            <input type="password" name="password" placeholder="Enter pass" />
            <input type="submit" />
        </form>`);
    })

发布请求 -

app.post('/login', (req,res) => {
        const { email, password } = req.body;
        console.log(email)
        console.log(password)
        console.log(users)
        let user1 = users.find((user) => user.email === email && user.password === password);
        console.log(user1);
    
        if(user1) {
                req.session.userId = user.id
                return res.redirect('/home')
        }
    
        res.redirect('/')
    })

问题 - 用户 1 未定义。

【问题讨论】:

    标签: node.js express session-cookies


    【解决方案1】:
    // Parse URL-encoded bodies (as sent by HTML forms)
    app.use(express.urlencoded());
    
    // Parse JSON bodies (as sent by API clients)
    app.use(express.json());
    

    这两行将确保您的服务器端将接收来自 req.body 的数据。 首先,您将数据作为表单数据发送,因此您需要包含

    // Parse URL-encoded bodies (as sent by HTML forms)
    app.use(express.urlencoded());
    

    这将允许您从 req.body 读取数据。

        app.post('/login', (req,res) => {
        const { email, password } = req.body;
        console.log('req',req.body)
        console.log("email",email)
        console.log("password",password)
        let index = users.findIndex((user) => user.email === email  && user.password == password);
        console.log("index",index)
        let user1= users[index]
        console.log("user1",user1);
    
        if(user1) {
            console.log("HERE=======================")
                req.session.userId = user1.id
                return res.redirect('/home')
        }
    
        res.redirect('/')
    })
    

    现在试试上面的代码快照,希望对你有帮助。

    【讨论】:

      【解决方案2】:

      我会说在req.body 中收到的password 值是string。您的=== 比较器还会检查变量type,而stringnumber 将不匹配。

      选项 1

      我建议将您的用户数据 password 属性更改为 string

      const users = [
        { id: 1, name: 'Sherry', email:'sherry@gmail.com', password: '1234' },   
        { id: 2, name: 'harry', email:'harry@gmail.com', password: '1234' },
        { id: 3, name: 'cherry', email:'cherry@gmail.com', password: '1234' }
      ]
      

      选项 2

      如果您只想使用数字密码,您还可以更改 find 函数以正确比较数字:

      let user1 = users.find((user) => user.email === email && user.password === parseInt(password));
      
      

      如果有效,请告诉我。

      【讨论】:

        猜你喜欢
        • 2017-08-16
        • 2020-08-21
        • 1970-01-01
        • 1970-01-01
        • 2016-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多