0

can someone help me debug this issue?

I am at the VueJS course and I have this weird issue (it happens even if I copy-paste source code from the course and it seems like the guy does not have it).

So here is the app image: enter image description here

When I click on teams in the navigation it opens me all the available teams with some info and the option to see members of each team.

enter image description here

When I click on Team 2 it should go to team 2 and its details.

Here is the working code:

<template>
<section>
    <h2>{{ teamName }}</h2>
    <ul>
        <user-item
                v-for="member in members"
                :key="member.id"
                :name="member.fullName"
                :role="member.role"
        ></user-item>
    </ul>
</section>
<router-link to="/teams/t2">Team 2</router-link>
</template>

<script>
    import UserItem from '../users/UserItem.vue';

    export default {
        components: {
            UserItem
        },
        inject: ['users', 'teams'],
        data() {
            return {
                teamName: '',
                members: []
            };
        },
        methods: {
            loadMembersData(route) {
                const teamId = route.params.teamId;
                const selectedTeam = this.teams.find((team) => team.id === teamId);
                const foundMembers = selectedTeam.members;
                const selectedMembers = [];

                for (const member of foundMembers) {
                    const selectedUser = this.users.find((user) => user.id === member);
                    selectedMembers.push(selectedUser);
                }

                this.members = selectedMembers;
                this.teamName = selectedTeam.name;
            }
        },
        created() {
            this.loadMembersData(this.$route);
        },
        watch: {
          $route(newRoute) {
              this.loadMembersData(newRoute);
          }
        }
    };
</script>

<style scoped>
    section {
        margin: 2rem auto;
        max-width: 40rem;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
        padding: 1rem;
        border-radius: 12px;
    }

    h2 {
        margin: 0.5rem 0;
    }

    ul {
        list-style: none;
        margin: 0;
        padding: 0;
    }
</style>

But when I add watcher then my navigation does not work and I get this issue:

enter image description here

Any ideas on how to solve this?

Thanks

Note: I am working with VueJS 3 if it means anything :)

SOLVED:

methods: {
        loadMembersData(route) {
            const teamId = route.params.teamId;
            const selectedTeam = this.teams.find((team) => team.id === teamId);
            if (selectedTeam) {
                const foundMembers = selectedTeam.members;
                const selectedMembers = [];

                for (const member of foundMembers) {
                    const selectedUser = this.users.find((user) => user.id === member);
                    selectedMembers.push(selectedUser);
                }

                this.members = selectedMembers;
                this.teamName = selectedTeam.name;
            }
        }
    },
8
  • 1
    I suspect that in loadMembersData selectedTeam can be undefined causing that issue, did you check that? :) Commented Jun 26, 2022 at 10:28
  • 1
    selectedTeam can be undefined. You need to check it first Commented Jun 26, 2022 at 10:31
  • @DarioPiotrowicz well I have added a check and it is okay now :D Can you please tell me how did you figure it out? Still learning it and I don't get it hahah :D Seems fine to me Commented Jun 26, 2022 at 10:33
  • 1
    how i figure it out? By reading docs. Literally at the top it says: If no values satisfy the testing function, undefined is returned. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jun 26, 2022 at 10:40
  • 1
    Ah yeah I also forgot to mention what @bill.gates said, if the find function doesn't find a match then it returns undefined :) (that's important of course, but if you didn't know that or forgot it, you'd arrive to that conclusion after following the kind-of strategy I mentioned earlier) Commented Jun 26, 2022 at 12:46

1 Answer 1

1

Solution that solved the issue:

methods: {
        loadMembersData(route) {
            const teamId = route.params.teamId;
            const selectedTeam = this.teams.find((team) => team.id === teamId);
            if (selectedTeam) {
                const foundMembers = selectedTeam.members;
                const selectedMembers = [];

                for (const member of foundMembers) {
                    const selectedUser = this.users.find((user) => user.id === member);
                    selectedMembers.push(selectedUser);
                }

                this.members = selectedMembers;
                this.teamName = selectedTeam.name;
            }
        }
    },
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.