Home > database >  JS says an object is undefined even though it shows with console.log
JS says an object is undefined even though it shows with console.log

Time:07-28

As the title states, my JS says an object is undefined even though if I console.log the parent it shows. I'm using prisma, but all that does is return a list of the objects containing {id, title, user:{id, name}}. Code:

const userProjects = await prisma.projectMembers.findMany({
  where: {
    userId: token._id
  },
  select: {
    project: {
      select: {
        id: true,
        title: true,
        user: {
          select: {
            id: true,
            name: true,
          },
        },
      },
    },
  },
});
userProjects.map(project => {
  console.log(project)
  console.log(project.user)
  return {
    id: project.id,
    title: project.title,
    user: project.user.id,
  }
})

Output:

Console.log output

CodePudding user response:

As you can see in the screenshot, there's a nested project property, and the user property is inside that. So project.user should be project.project.user.

userProjects.map(project => {
  console.log(project)
  console.log(project.project.user)
  return project.project;
})

There's no need for you to create your own object when returning, since it's the same as project.project.

  • Related