Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript返回没有返回任何内容_Javascript_Node.js - Fatal编程技术网

Javascript返回没有返回任何内容

Javascript返回没有返回任何内容,javascript,node.js,Javascript,Node.js,我有一个NodeJS服务器和一个发出HTTP请求的助手函数,但是当我调用该函数时,它被认为是未定义的。调用是在回调中进行的,因此我认为它的异步部分没有问题 这是server.js console.log(dictionary('wut')); 这是函数字典 if(dictionary[word]===undefined){ request({ url:"https://mashape-community-urban-dictionary.p.mashape.com/de

我有一个NodeJS服务器和一个发出HTTP请求的助手函数,但是当我调用该函数时,它被认为是未定义的。调用是在回调中进行的,因此我认为它的异步部分没有问题

这是server.js

console.log(dictionary('wut'));
这是函数字典

if(dictionary[word]===undefined){
    request({
        url:"https://mashape-community-urban-dictionary.p.mashape.com/define?term="+word,
        headers:{
            'X-Mashape-Key':'KEY HERE',
            'Accept':'text/plain'
        }
    }, function(err, response, body){
            if(!err && response.statusCode==200){
                var info = JSON.parse(body);
                console.log(info.list[0].definition);
                return info.list[0].definition;
            }
    });
} else {
    return dictionary[word];
}
其中word是传递给函数的单词

编辑:我忘了提到dictionary函数

module.exports = function(word){

return语句应该为模块提供回调中的值。很抱歉,这是一种重要的信息。

从我的观点来看,您使用的
请求
库似乎是异步设计的。这意味着您应该处理回调函数内部的数据

例如:

function handle(data) {
  // Handle data
  console.log(data)
}

if(dictionary[word]===undefined){
  request({
    url:"https://mashape-community-urban-dictionary.p.mashape.com/define?term="+word,
    headers:{
        'X-Mashape-Key':'KEY HERE',
        'Accept':'text/plain'
    }
  }, function(err, response, body){
    if(!err && response.statusCode==200){
      var info = JSON.parse(body);
      console.log(info.list[0].definition);
      handle(info.list[0].definition)
    }
  });
} else {
  handle( dictionary[word] )
}
你没有提供足够的信息让我正确设置。但希望这能让你知道你需要做什么


要详细说明为什么要这样设置:

  • 请求函数似乎是异步的,所以请保持它的设计方式
  • 您返回的是回调内部,因此您的外部
    字典
    函数没有获取返回的数据,而回调是
  • 由于
    请求
    函数是异步设计的,因此没有一种方法可以将数据返回到
    字典
    而不强制同步(不建议这样做)。因此,您应该将其重新构造为在回调内部处理
    (另一个小注意事项是,您应该使用
    typeof dictionary[word]==“undefined”
    ,因为我相信JavaScript有时会抛出错误。)

    您将希望在助手方法中使用回调方法

    因此,您的助手定义如下所示:

    function dictionary(word, callback) {
        request({}, function(err, res, body) {
            if (err) {
                return callback(err);
            }
    
            callback(null, body);
        });
    }
    
    你的电话会变成:

    dictionary('wut', function(err, result) {
        if (err) {
            return console.error('Something went wrong!');
        }
    
        console.log(result);
    });
    
    这显然是一个非常简单的实现,但概念是存在的。您的助手/模块/任何内容都应该编写为接受回调方法,然后您可以使用这些方法来冒泡错误并在应用程序中的适当位置处理它们。这几乎是在Node中执行操作的标准方法

    以下是您如何使用简单的快速路线呼叫您的助手:

    router.route('/:term')
        .get(function(req, res) {
            dictionary(req.params.term, function(err, result) {
                if (err) {
                    return res.status(404).send('Something went wrong!');
                }
    
                res.send(result);
            });
        });
    

    什么是
    退货信息列表[0]。定义应该在异步回调中执行吗?这是一个异步函数。您需要一个回调函数,该函数应使用
    info.list[0]调用。定义
    而不是仅返回
    info.list[0]。定义
    (它不起任何作用)。很抱歉,我忘记添加:当我执行
    返回info.list[0]时,定义,它记录答案的定义,因此这是一个字符串。那么,它不应该通过请求回调并返回该字符串吗?