额外weired:在节点中的进程中间重置变量值



我警告你,这是额外的重量。

我有以下代码,我从请求中获得complaintId值,然后以某种方式重置其值:

[ 'rp.args ', '60eeba1e1e6ee73bfc4b1350' ]
[ 'found counter by cid: ', '60eeba1e1e6ee73bfc4b1350', ': ', null ]
[ 'finding complaint by id: ', undefined ]
[ 'found complaint by id: ', undefined, ': ', null ]

这个输出来自这里:

const complaintById = ComplaintTC.mongooseResolvers.findById().wrapResolve(next => async rp => {
const complaintId = rp.args._id
logger.debug("rp.args ", complaintId)
let counter = await ComplaintVisitCount.findOne({complaintId})
logger.debug("found counter by cid: ", complaintId, ": ", counter)
if( counter == null ) {
logger.debug("finding complaint by id: ", complaintId)
const complaint = await Complaint.findById(complaintId)
logger.debug("found complaint by id: ", complaintId, ": ", complaint)
const {entityGroupId, entityId, _id:complaintId} = complaint
counter = {entityGroupId, entityId, complaintId, visits:1}
ComplaintVisitCount.create(counter)
} else {
counter.visits++
await counter.save()
}
const toReturn = await next(rp);
toReturn.stats.visits = counter.visits
logger.debug("returning complaint with ", counter.visits, " visits: ", toReturn )
return toReturn
})

有什么解释吗?!!!?

注意:我使用的是graphql compose mongose,这就是我使用它的原因关于问题的样板代码

好的,

这是由于当异常发生时,Node变得不稳定,行

const {entityGroupId, entityId, _id:complaintId} = complaint

导致了一个异常,并且正在重置变量,此外,console.log不会立即执行,它被放在一个单独的"线程"中,所以当它执行时,它发现变量为空

因此,为了解决这个问题,我不得不更改这一行(由于不明原因,它抛出了"无法解析为null的entityGroupId",但当我通过其getter访问它时效果很好(

// const {entityGroupId, entityId, _id:complaintId} = complaint
counter = {
entityGroupId: complaint.entityGroupId,
entityId: complaint.entityId,
complaintId: complaint._id,
visits: 1
}

最新更新