微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Chrome 节点检查器导致代码执行不同?

如何解决Chrome 节点检查器导致代码执行不同?

我遇到了一个奇怪的问题,根据 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..

这是一个在 routeHandler 中调用函数

但是当我使用 --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 举报,一经查实,本站将立刻删除。