Javascript - 通过 console.log 调试

Javascript - debug via console.log

我在js中有这部分代码:

getResults() {
    let par = Object.keys(this.selected_options).map((key)=> {
        return key + '=' + encodeURIComponent(this.selected_options[key]);
    }).join('&');
    console.log(par);
    this.search(par);
}

search(par){
    this.http.get('check?'+par)
    .then(response => this.helper.isJson(response.response) || [])
    .then(data => this.news = data)
    .catch(error => {
        let response = this.helper.isJson(error.response);
        let message = this.helper.getErrorMessage(error.statusCode, response);
    });
}

问题是 console.log 在任何地方都不起作用。我需要调试一下,查看response和data的内容。

即使我尝试使用 console.log(par);正如您在我的代码中看到的那样它说意外的控制台语句(无控制台)。

在第二部分甚至没有地方放 console.log 因为整个请求都是用 .then.

连接在一起的

那么我该如何调试这样的代码呢?我的意思是在第二部分我需要查看数据结果以及响应。

即使我用

// eslint-disable-next-line no-console
console.log(response); 

在搜索方法的最后,它不起作用。

您正在使用内联 lambda 函数,如果只有一个语句,它可以省略 {}。如果你想要更多的声明,你可以添加它们:

.then(response => this.helper.isJson(response.response) || [])

可以

.then(response => {
       console.log(response);
       return this.helper.isJson(response.response) || []);
 })

不要忘记添加 return 语句!