简体   繁体   English

调试Javascript / ReactJS错误

[英]Debugging Javascript/ReactJS errors

I'm building a small application with ReactJS and sometimes find it difficult to debug it. 我正在用ReactJS构建一个小应用程序,有时候发现很难调试它。

Every time I make some Javascript error , like missing let/var in front of new variable, missing require for a component that I later use, my application just stops working (the code does not execute beyond the line where the error is), but I'm not getting any errors in browser's console. 每次我做一些Javascript error ,如缺少let/var在新的变数面前,缺少require的,我以后使用的成分,我的应用程序只是停止工作(代码不执行超出错误所在行),但我在浏览器的控制台中没有收到任何错误。 It seems as if some ReactJS code was intercepting errors, maybe handling them some custom way. 似乎有些ReactJS代码拦截了错误,可能会以某种自定义方式处理它们。 Is there anything like that in ReactJS? 在ReactJS中有类似的东西吗? How can I see errors in the console? 如何在控制台中看到错误?

I'm using gulp/gulp-connect/browserify set to run the application. 我正在使用gulp/gulp-connect/browserify设置来运行应用程序。

Let me know if you need any additional data or code samples, I'll update the question. 如果您需要任何其他数据或代码示例,请告诉我,我会更新问题。

If you know that an error is thrown but swallowed by some other code, you can enable in your browser's debugger to pause execution when an exception is thrown, even if it is caught somewhere: 如果您知道某个错误被抛出但被某些其他代码吞没,您可以在浏览器的调试器中启用,以便在抛出异常时暂停执行,即使它被某处捕获:

在此输入图像描述

Note however that libraries sometimes deliberately trigger exceptions while testing whether the browser supports certain features. 但请注意,在测试浏览器是否支持某些功能时,库有时会故意触发异常。 You'd have to step over those. 你必须跨过那些。

Usage of React Hot Loader which is a Webpack plugin should solve most of the problems you have in a development process. 使用React Hot Loader是一个Webpack插件,可以解决开发过程中遇到的大多数问题。 It's easy to integrate into existing project and has quite a few examples how to put all the things together. 它很容易集成到现有项目中,并且有很多例子可以将所有内容放在一起。

As a result: 结果是:

  • your code changes would be pushed to the browser 您的代码更改将被推送到浏览器
  • in case of the error you will have meaningful stack trace in the browser console. 如果出现错误,您将在浏览器控制台中获得有意义的堆栈跟踪。

I'm guessing that the incorrect JS syntax is causing your gulp process to fail which would result in your application not being bundled/deployed in the browser at all. 我猜测不正确的JS语法导致你的gulp进程失败,这会导致你的应用程序根本没有捆绑/部署在浏览器中。

It seems like there should be errors in your system console (where gulp is running) - as opposed to your browser console. 似乎系统控制台(gulp正在运行)中应该存在错误 - 而不是浏览器控制台。 Possibly your gulp process crashes when this happens too. 当这种情况发生时,你的吞咽过程可能会崩溃。 If there are no errors in your console, you may have to listen for them. 如果您的控制台中没有错误,您可能必须听取它们。 This post has an example of how to log errors from browserify: 这篇文章有一个如何从browserify记录错误的例子:

gulp.task('browserify', function(){
  var b = browserify();
  b.add('./main.js');

  return b.bundle()
    .on('error', function(err){
      console.log(err.message); //
      this.end();
    })
    .pipe(source('main.out.js'))
    .pipe(gulp.dest('./dist'));
});

Probably the best solution is to run your code through jshint before browserify so that you aren't trying to browserify syntactically invalid code. 可能最好的解决方案是在浏览器之前通过jshint运行代码,这样您就不会尝试浏览语法无效的代码。

Caveat: not a current gulp user 警告:不是当前的gulp用户

I suffered from the similar problem with this like missing let/var, require, and other trivial mistakes inside of the react context. 我遇到了类似的问题,比如在反应上下文中缺少let / var,require和其他琐碎的错误。 In my case, the cause is the mistake of Promise statement. 就我而言,原因是Promise声明的错误。 Promise seems to suppress any exceptions that occur after it is called. Promise似乎可以抑制在调用之后发生的任何异常。 I could resolve the problem by handling exception like below. 我可以通过处理下面的异常来解决问题。

var Promise = require('es6-promise').Promise;
var promise = new Promise(...)
promise
  .then(function(data){...})
  .catch(function(e) {
    console.error(e.stack)
}

react-slingshot is a starter kit and it shows error at compile time and shows stack trace on the browser. react-slingshot是一个入门套件,它在编译时显示错误,并在浏览器上显示堆栈跟踪。 It also has testing set up. 它也有测试设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM