我想訪問一個物件的密鑰,但它回傳未定義,我不知道為什么
這是我的代碼
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
回報

uj5u.com熱心網友回復:
我嘗試使用 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 ]
]
我在buyUsers 查詢中添加了精益選項并解決了我的問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/343598.html
標籤:javascript 打字稿 猫鼬 嵌套
