You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React项目中登录验证成功后提示框显示用户名undefined的问题求助

问题分析与解决方案

Hey there! The issue you're seeing (Welcome undefined) comes down to how you're validating users and accessing their name. Let's break it down and fix it:

What's Wrong?

You're using Array.some() to check for a valid user, which only returns a boolean (true if a matching user exists, false otherwise). It doesn't actually give you the specific user object that matched the credentials.

When you try to use Users.name, you're trying to access a name property directly on the Users array itself—arrays don't have a name property, so you end up with undefined.

The Fix

Replace Array.some() with Array.find(). Unlike some(), find() returns the first matching user object (or undefined if no match is found). This lets you access the user's name property correctly.

Here's your updated login function:

const login = (details) => {
  // Find the exact user that matches the email and password
  const matchedUser = Users.find((user) => {
    return user.email === details.email && user.password === details.password;
  });

  if (matchedUser) {
    // Now we can safely access the matched user's name
    toast.success(`Welcome ${matchedUser.name}`);
    navigate("/");
  } else {
    toast.error("臘‍♂️ Invalid email or password! Please check your entries.");
  }
};

Bonus Tip

If you plan to use the logged-in user's information elsewhere in your app, consider storing matchedUser in a React state (using useState). This way, you can access the user's details across components instead of just showing it in a toast.

内容的提问来源于stack exchange,提问作者Tony

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 21:57:47