【问题标题】:Why is this an unreachable code?为什么这是一个无法访问的代码?
【发布时间】:2016-04-01 17:59:04
【问题描述】:

我正在从另一个函数调用函数 getKeywords 并得到一个 Unrecheable code detected 部分,但不明白为什么。有什么帮助吗?

var env         = require('dotenv').config();
var request     = require('request')
var getKeywords = function(){
request.get('URI', //URI IS CORRECT IN MY CODE
function(err, httpResponse, body){
    if(err){ //UNREACHABLE CODE DETECTED
        console.error("request.post Error:", err);  
        return false;  
    } //UNREACHABLE CODE DETECTED
    else{
        console.log('Im here');
        return JSON.parse(httpResponse.body).keywords;
    }
 });
}

module.export = getKeywords;

这是调用代码。

  var getKeywords  = require('./getKeywords.js');
  var keywords     = new getKeywords();
  var env          = require('dotenv').config();
  var difflib      = require('difflib');
  var postMention  = require('./postMention.js');

 var detection   = function(obj, i){
       var keyword = keywords[i];
            var mentionObject = {
                //some json
            //postMention(mentionObject);
        }

 }     
  module.exports = detection;

【问题讨论】:

  • 您使用什么工具来识别无法访问的代码?
  • 随便什么工具,我觉得这里搞错了。
  • 我在 ubuntu 14.04 上使用 Visual Studio 代码。问题是我 console.log() 它并没有输出任何东西。
  • 您还有一些其他问题没有在您的真实代码中向我们披露(字符串不匹配或其他错误),或者这是您的工具中的错误错误。您向我们展示的代码没有任何问题,与显示的完全一样。
  • 加载这个模块后,你是不是真的在这里调用了名为getKeywords()的模块构造函数?如果您将console.log() 作为getKeywords() 的第一行,它会被调用吗?你能告诉我们调用代码吗?

标签: javascript node.js get


【解决方案1】:

某些工具能够分析对您的函数的每次调用。您的代码中调用该函数的所有位置都可能从未将 err 参数设置为 true。

【讨论】:

    猜你喜欢
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 2014-04-29
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多