问题描述
我遇到了一个奇怪的问题,根据 Chrome devtools 是否打开,某些代码采用不同的路径......或者至少看起来是这样。
示例
Transaction
只是一个简单的猫鼬模型。我正在使用 MongoDB Atlas。
import Transaction from '../models/Transaction'
export default async function sendMessage({ textMsg,recipient,event }) {
debugger;
// pause here to inspect DB
try {
const { transactionHash } = event
// transactionHash is a string
const existingTx = await Transaction.exists({ transactionHash })
// don't want dupe transactions,check if hacker is trying `replay` an old transaction..
if (existingTx) {
console.log('This transaction has already happened')
// tx was already sent,early return..
return
}
const transactionRecord = new Transaction({textMsg,recipient})
await transactionRecord.save()
... Else send the message..
但是当我使用 --inspect
运行 Node/Expressjs 服务器并打开 Chrome 节点检查器 devtools 时,发生了一些奇怪的事情:当检查器在第 1 行(debugger;
语句)暂停时,一条 Mongo 记录for Transaction (transactionHash) 已经创建。它甚至没有到达 transactionRecord.save()
所在的位置。因此,existingTx
总是返回 true 并且函数提前返回。
当 Node 调试器关闭时不会发生这种情况
使用 --inspect
运行节点 但未打开 Chrome 节点检查器 devtools 永远不会提前返回。 existingTx
变量返回 false,不提前返回,保存一条 mongo 记录,并发送一条消息(函数的最终目标)。
为什么要这样做? 我一直在绞尽脑汁想知道这是为什么。我以为是sourcemaps的问题,但即使是sourcemaps的问题也不会导致代码走不同的路径。
有没有遇到过类似的情况?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)