即使值存在,为啥对象键返回未定义

Posted

技术标签:

【中文标题】即使值存在,为啥对象键返回未定义【英文标题】:Why object key returns undefined even though value exists即使值存在,为什么对象键返回未定义 【发布时间】:2021-12-13 15:56:26 【问题描述】:

我想访问一个对象的键,但它返回未定义,我不知道为什么

这是我的代码

const docs = await this.walletModel.find(
      status: 'Venus'
    ).select('user meteorite').populate('user', 'name extra.profilePic extra.thumbnail').lean()

    // @ts-ignore
    const userIds = docs.map(user => Types.ObjectId(user?.user?._id))

    const boughtUsers = await this.subscriptionModel.find( user:  $in: userIds  ).select('-_id user')

    const reduced = docs.reduce((acc, curr) => 
      // @ts-ignore
      if (boughtUsers.some(boughtUser => 
        // @ts-ignore
        console.log(boughtUser, curr?.user?._id) // logging here

        // @ts-ignore
        return boughtUser?.user === curr?.user?._id
      )) return [...acc, ...curr]

      return [...acc, ...curr, gift: true]
    , [])

    return reduced

返回

您可以看到有用户密钥,但是当我尝试记录它时它返回未定义

代码

const docs = await this.walletModel.find(
      status: 'Venus'
    ).select('user meteorite').populate('user', 'name extra.profilePic extra.thumbnail').lean()

    // @ts-ignore
    const userIds = docs.map(user => Types.ObjectId(user?.user?._id))

    const boughtUsers = await this.subscriptionModel.find( user:  $in: userIds  ).select('-_id user')

    const reduced = docs.reduce((acc, curr) => 
      // @ts-ignore
      if (boughtUsers.some(boughtUser => 
        // @ts-ignore
        console.log(boughtUser?.user, curr?.user?._id) // logging here

        // @ts-ignore
        return boughtUser?.user === curr?.user?._id
      )) return [...acc, ...curr]

      return [...acc, ...curr, gift: true]
    , [])

    return reduced

返回

【问题讨论】:

typeof购买的用户是什么?例如console.log(typeof boughtUser) 它正在返回对象 console.log(boughtUser.constructor.name) 会给出类型。你试过 console.log(JOSN.stringify(boughtUser)) 吗?我认为您出于某些原因添加了 // @ts-ignore。我可以想象删除它会给出类型警告,您可以使用它来了解您的类型出了什么问题。 【参考方案1】:

我尝试使用 Object.entries() 进行日志记录,它返回了类似的内容

[
  [
    '$__',
    InternalCache 
      strictMode: false,
      selected: [Object],
      shardval: undefined,
      saveError: undefined,
      validationError: undefined,
      adhocPaths: undefined,
      removing: undefined,
      inserting: undefined,
      saving: undefined,
      version: undefined,
      getters: ,
      _id: undefined,
      populate: undefined,
      populated: undefined,
      wasPopulated: false,
      scope: undefined,
      activePaths: [StateMachine],
      pathsToScopes: ,
      cachedRequired: ,
      session: null,
      '$setCalled': Set(0) ,
      ownerDocument: undefined,
      fullPath: undefined,
      emitter: [EventEmitter],
      '$options': [Object]
    
  ],
  [ 'isNew', false ],
  [ 'errors', undefined ],
  [ '$locals',  ],
  [ '$op', null ],
  [ '_doc',  user: 6179847*******104c5715ee  ],
  [ '$init', true ]
]

我在购买用户查询中添加了精益选项并修复了我的问题

【讨论】:

以上是关于即使值存在,为啥对象键返回未定义的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的函数向返回未定义的对象数组添加键和增量值

为啥 map() 返回一个未定义值的数组?

为啥 find() 返回未定义?

为啥 find() 返回未定义?

使用 jquery 访问设置为 attr 的 Mongoose 模式键(布尔值),但它返回未定义

为啥在第一次组件渲染中未定义 useState 对象值?