简体   繁体   English

JS 说 object 是未定义的,即使它显示在 console.log 中

[英]JS says an object is undefined even though it shows with console.log

As the title states, my JS says an object is undefined even though if I console.log the parent it shows.正如标题所述,我的 JS 说 object 是未定义的,即使我 console.log 它显示的父级也是如此。 I'm using prisma, but all that does is return a list of the objects containing {id, title, user:{id, name}} .我正在使用 prisma,但所做的只是返回包含{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: Output:

Console.log 输出

As you can see in the screenshot, there's a nested project property, and the user property is inside that.正如您在屏幕截图中看到的那样,有一个嵌套的project属性, user属性在其中。 So project.user should be project.project.user .所以project.user应该是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 .返回时无需创建自己的 object ,因为它与project.project相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React state 未定义,即使 console.log 显示它 - React state is undefined , even though console.log shows it 即使对象上的console.log显示属性值,对象的属性也未定义 - Property of object is undefined, even though console.log on object is showing the property value 变量是未定义的错误(即使console.log显示变量) - Variable is undefined error (even if console.log shows variable) 即使console.log清楚地显示了具有所需属性的对象,也无法访问对象属性(React Redux) - Can't access object property even though console.log clearly shows an Object with the needed property (React Redux) 过滤器没有返回数组的第一个元素,即使 console.log 说它是 - Filter is not returning the first element of the array, even though console.log says it is Backbone.js model.get()返回'undefined',即使我可以在console.log中看到属性 - Backbone.js model.get() returning 'undefined' even though I can see the attributes in console.log 为什么我console.log一个对象,它显示对象,但是当我console.log时,它显示未定义 - Why I console.log a Object,it shows object,but when I console Object.value ,it shows undefined 即使之前的 console.log 显示该语句应该运行,if 语句也不会运行 - if statement doen't run even though the console.log before that shows the statement should run Discord.js 表示“消息为空”但 console.log 显示数据 - Discord.js says "message is empty" but console.log shows data javascript函数即使在console.log中显示值也返回未定义 - javascript function returning undefined even though value display in console.log
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM