为什么 object 键 returns 未定义,即使值存在
Why object key returns undefined even though value exists
我想访问 object 的密钥,但它 returns 未定义,我不知道为什么
这是我的代码
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
它returns
如您所见,有用户密钥,但是当我尝试记录它时 returns 未定义
代码
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
returns
我尝试使用 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 ]
]
我向 boughtUsers 查询添加了 lean 选项并解决了我的问题
我想访问 object 的密钥,但它 returns 未定义,我不知道为什么
这是我的代码
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
它returns
如您所见,有用户密钥,但是当我尝试记录它时 returns 未定义
代码
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
returns
我尝试使用 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 ]
]
我向 boughtUsers 查询添加了 lean 选项并解决了我的问题