Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.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 从JS/Node模块获取返回值_Javascript_Node.js_Mocha.js - Fatal编程技术网

Javascript 从JS/Node模块获取返回值

Javascript 从JS/Node模块获取返回值,javascript,node.js,mocha.js,Javascript,Node.js,Mocha.js,我创建了一个简单的模块,将一些数据发布到一个外部服务,该服务返回一条消息和一些其他结果 我试图用摩卡来测试,但我发现很难理解如何访问返回值 我可以在控制台中看到它的日志,但不知道如何将其设置为变量。您一定会注意到,我是Javascripter的新手。我相信这很简单,我就是不知道怎么做 我的模块: module.exports = { foo: function(id,serial) { var querystring = require('querystring'); va

我创建了一个简单的模块,将一些数据发布到一个外部服务,该服务返回一条消息和一些其他结果

我试图用摩卡来测试,但我发现很难理解如何访问返回值

我可以在控制台中看到它的日志,但不知道如何将其设置为变量。您一定会注意到,我是Javascripter的新手。我相信这很简单,我就是不知道怎么做

我的模块:

module.exports = {

  foo: function(id,serial) {
    var querystring = require('querystring');
    var http = require('http');
    var fs = require('fs');
    var post_data = querystring.stringify({
        'serial' : serial,
        'id': id
    });

    var post_options = {
        host: 'localhost',
        port: '8080',
        path: '/api/v1/status',
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Content-Length': post_data.length
        }
    };

    var post_req = http.request(post_options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log(chunk);
            return chunk;
        });
    });

    post_req.write(post_data);
    post_req.end();
  }
}
我把这个叫做:

describe('Functions and modules', function() {
  it('does some shizzle', function(done) {
    var tools = require('../tools');
    chunk = '';
    id = 123;
    serial =456;
    tools.foo(id,serial);
    chunk.should.equal.......
  });
});
我基本上需要从tools.foo(id,serial)返回的消息,但是chunk比一个空白的东西更空白

在我的终端中,我可以看到如下内容:

{"message":"This device is still in use","live":"nop"}
不能像在其他语言中那样访问“返回”值。节点中的Http请求是异步的,不返回它们的值。而是传入回调函数,或在同一请求的范围内创建回调函数。例如,您可以这样完成您的函数:(我删除了一些填充)


如果您定义的函数在同一范围内,您也可以直接在foo的范围内访问someCallBackFunction,但是传入回调是更好的方式。

Ah,我是一个rubyst;)好的,我现在就来试一试。干杯,我现在明白重点了。但是,这给了我一个错误:“TypeError:undefined不是一个函数”引用Callback这意味着您没有向foo传递足够的参数。您需要向“callback”参数传递一个变量,该变量已定义为一个接受一个参数的函数。我用对foo的示例调用修改了我的示例。
module.exports = {

    foo: function (options, data, callback) {
        'use strict';

        var completeData = '';

        var post_req = http.request(options, function (res) {
            res.setEncoding('utf8');

            res.on('data', function (chunk) {
                console.log(chunk);
                completeData += chunk;
                return chunk;
            });

            res.on('end', function () {
                callback(completeData);
                //You can't return the data, but you can pass foo a callback,
                //and call that function with the data you collected
                //as the argument.
            });

        });

        post_req.write(data);
        post_req.end();
    }
};

function someCallBackFunction (data) {
    console.log("THE DATA: " + data);
}

var someOptions = "whatever your options are";
var someData = "whatever your data is";

module.exports.foo(someOptions, someData, someCallBackFunction);