英文:
Filtered array always empty
问题
我尝试进行一些筛选逻辑并获取我所在项目的成员(根据URL参数中的项目名称),但问题是,如您所见,它总是空的(即使我将筛选条件更改为明显的条件)。我还尝试了其他方法来替代对allUsers数组的筛选,它们都能正常工作。
function Team({ setShowAddMembers }: props) {
const params = useParams();
const { projectName } = params;
const [projectTeam, setProjectTeam] = useState<user[]>([]);
useEffect(() => {
try {
const getProjectTeam = async () => {
const allProjects: project[] = await getAllProjects();
console.log(allProjects); // 正常工作。记录项目数组
const allUsers: user[] = await getAllUsers();
console.log(allUsers); // 正常工作。记录用户数组
// 仅获取当前页面的项目
const currentProject: project[] = allProjects.filter(
(project) => project.name === projectName
);
console.log(currentProject); // 正常工作。记录一个包含1项的数组
console.log(currentProject[0]); // 正常工作
const { members } = currentProject[0];
console.log(members); // 正常工作。记录当前项目成员的数组
const teamMembers: user[] = allUsers.filter((user) =>
members.includes(user.username)
);
console.log(teamMembers); // 不起作用。记录空数组
setProjectTeam(teamMembers);
};
getProjectTeam();
} catch (error) {
console.log(error);
}
}, []);
}
控制台日志(按照代码中的顺序):
- allProjects: 一个包含7个此类型对象的数组:
{
"tickets": [],
"_id": "640f80cbd601d54a956de834",
"desc": "Marketplace website",
"name": "Market",
"members": [
"John Doe",
"Dave Gray"
]
}
- allUsers: 包含此类型对象的数组:
{
"_id": "640c6e87d8efde2fabbb1ba2",
"username": "AdminDemo",
"email": "admindemo@gmail.com",
"password": "$2b$10$7O7JZVgYW9k0dOXiAFtWv.jN7VVmoooPagOvnEnfEfSB7cTH0md/S",
"role": "admin",
"protected": true,
"__v": 0
}
- current project:
[
{
"tickets": [],
"_id": "640f80cbd601d54a956de834",
"desc": "Marketplace website",
"name": "Market",
"members": [
"John Doe",
"Dave Gray"
]
}
]
- currentProject[0]:
{
"tickets": [],
"_id": "640f80cbd601d54a956de834",
"desc": "Marketplace website",
"name": "Market",
"members": [
"John Doe",
"Dave Gray"
]
}
- members:
["John Doe", "Dave Gray"]
- teamMembers:
[]
英文:
I tried to do some filtering logic and get the members of the project im on ( according to its name on the url params ), but the problem is that it's always empty as you can see below ( teamMembers is always empty even if i change the filtering condition to something obvious).
I also tried other methods instead of filter on the allUsers array, and they just fine somehow .
function Team({ setShowAddMembers }: props) {
const params = useParams();
const { projectName } = params;
const [projectTeam, setProjectTeam] = useState<user[]>([]);
useEffect(() => {
try {
const getProjectTeam = async () => {
const allProjects: project[] = await getAllProjects();
console.log(allProjects); // works. Logs projects array
const allUsers: user[] = await getAllUsers();
console.log(allUsers); // works. Logs users array
// to get only the project of the page you're on
const currentProject: project[] = allProjects.filter(
(project) => project.name === projectName
);
console.log(currentProject); // works as intended. Logs a 1 item array
console.log(currentProject[0]); // works as intended
const { members } = currentProject[0];
console.log(members); //works. Logs array of the members of the current project
const teamMembers: user[] = allUsers.filter((user) =>
members.includes(user.username)
);
console.log(teamMembers); // DOES NOT WORK. LOGS EMPTY ARRAY
setProjectTeam(teamMembers);
};
getProjectTeam();
} catch (error) {
console.log(error);
}
}, []);
console logs: ( in order just like in the code )
- allProjects: [{…}, {…}, {…}, {…}, {…}, {…}, {…}] an array of 7 of this type of objects:
{
"tickets": [],
"_id": "640f80cbd601d54a956de834",
"desc": "Marketplace website",
"name": "Market",
"members": [
"John Doe",
"Dave Gray"
]
}
- allUsers: [{…}, {…}, {…}, {…}] an array of this type of objects:
{
"_id": "640c6e87d8efde2fabbb1ba2",
"username": "AdminDemo",
"email": "admindemo@gmail.com",
"password": "$2b$10$7O7JZVgYW9k0dOXiAFtWv.jN7VVmoooPagOvnEnfEfSB7cTH0md/S",
"role": "admin",
"protected": true,
"__v": 0
}
- current project:
[
{
"tickets": [],
"_id": "640f80cbd601d54a956de834",
"desc": "Marketplace website",
"name": "Market",
"members": [
"John Doe",
"Dave Gray"
]
}
]
- currentProject[0]:
{
"tickets": [],
"_id": "640f80cbd601d54a956de834",
"desc": "Marketplace website",
"name": "Market",
"members": [
"John Doe",
"Dave Gray"
]
}
- members:
["John Doe", "Dave Gray"]
- teamMembers:
[]
答案1
得分: 2
代码部分不翻译,以下是翻译好的部分:
"Looks like you issue is that you try to compare members
names e.g. John Doe
with the username of a user e.g. AdminDemo
in the way the examples are given I would bet that these will never match.
I dont know how you build the username compared to the members name, if its only removes the space between first and last name you can do a hacky solution by just removing the spaces from the members name.
However would recommend to store userIds as members instead, to mitigate further issues in the future with user having the same name and so on."
英文:
Looks like you issue is that you try to compare members
names e.g. John Doe
with the username of a user e.g. AdminDemo
in the way the examples are given I would bet that these will never match.
I dont know how you build the username compared to the members name, if its only removes the space between first and last name you can do a hacky solution by just removing the spaces from the members name.
const teamMembers: user[] = allUsers.filter((user) =>
members.some(m => m.replace(" ", "") === user.userName)
);
However would recommend to store userIds as members instead, to mitigate further issues in the future with user having the same name and so on.
答案2
得分: 1
我认为您可以通过查看您的成员(我假设是成员列表)来解决您的问题,并编写类似以下的代码:
members.some(m => m.username === user.username);
some
会遍历数组,如果条件返回 true,则返回 true,否则返回 false。
英文:
I think you could solve your problem by looking through your members (what I assume is a list of members.) And writing something in the lines of
members.some(m => m.username === user.username);
some looks through the array and returns if the condition retruns true, otherwise it returns false.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论