Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.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 module.exports中的Node.js我的变量定义不足_Javascript_Node.js_Module - Fatal编程技术网

Javascript module.exports中的Node.js我的变量定义不足

Javascript module.exports中的Node.js我的变量定义不足,javascript,node.js,module,Javascript,Node.js,Module,我的变量“整”未定义 getStudents.js var http = require('http'); module.exports = { 'sendResults': function(){ http.get(' http://gs-class.com/nodejs/students.php', function(res){ var whole = ''; res.on('data',function(chunk){ whole+=chunk;

我的变量“整”未定义

getStudents.js

var http = require('http');

module.exports = {
'sendResults': function(){
    http.get(' http://gs-class.com/nodejs/students.php', function(res){
    var whole = '';
    res.on('data',function(chunk){
        whole+=chunk;
    });
    res.on('end', function(){
        return whole;
    });
    });
    }
}
var getStudents = require('./getStudents');
var fs = require('fs');
var results = getStudents.sendResults();
console.log(results);
var getStudents = require('./getStudents');
var fs = require('fs');
getStudents.sendResults(function(result) {
    console.log(result);
});
var http = require('http');

    module.exports = {
        'sendResults': function(callback) {
            http.get(' http://gs-class.com/nodejs/students.php', function(res) {
                var whole = '';
                res.on('data', function(chunk) {
                    whole += chunk;
                });
                res.on('end', function() {
                    callback(whole)
                });
            });
        }
    }
studWrite.js

var http = require('http');

module.exports = {
'sendResults': function(){
    http.get(' http://gs-class.com/nodejs/students.php', function(res){
    var whole = '';
    res.on('data',function(chunk){
        whole+=chunk;
    });
    res.on('end', function(){
        return whole;
    });
    });
    }
}
var getStudents = require('./getStudents');
var fs = require('fs');
var results = getStudents.sendResults();
console.log(results);
var getStudents = require('./getStudents');
var fs = require('fs');
getStudents.sendResults(function(result) {
    console.log(result);
});
var http = require('http');

    module.exports = {
        'sendResults': function(callback) {
            http.get(' http://gs-class.com/nodejs/students.php', function(res) {
                var whole = '';
                res.on('data', function(chunk) {
                    whole += chunk;
                });
                res.on('end', function() {
                    callback(whole)
                });
            });
        }
    }
当我运行studWrite.js程序时,我的变量“整”是未定义的。请帮忙。

试试这个:

studWrite.js

var http = require('http');

module.exports = {
'sendResults': function(){
    http.get(' http://gs-class.com/nodejs/students.php', function(res){
    var whole = '';
    res.on('data',function(chunk){
        whole+=chunk;
    });
    res.on('end', function(){
        return whole;
    });
    });
    }
}
var getStudents = require('./getStudents');
var fs = require('fs');
var results = getStudents.sendResults();
console.log(results);
var getStudents = require('./getStudents');
var fs = require('fs');
getStudents.sendResults(function(result) {
    console.log(result);
});
var http = require('http');

    module.exports = {
        'sendResults': function(callback) {
            http.get(' http://gs-class.com/nodejs/students.php', function(res) {
                var whole = '';
                res.on('data', function(chunk) {
                    whole += chunk;
                });
                res.on('end', function() {
                    callback(whole)
                });
            });
        }
    }
getStudents.js

var http = require('http');

module.exports = {
'sendResults': function(){
    http.get(' http://gs-class.com/nodejs/students.php', function(res){
    var whole = '';
    res.on('data',function(chunk){
        whole+=chunk;
    });
    res.on('end', function(){
        return whole;
    });
    });
    }
}
var getStudents = require('./getStudents');
var fs = require('fs');
var results = getStudents.sendResults();
console.log(results);
var getStudents = require('./getStudents');
var fs = require('fs');
getStudents.sendResults(function(result) {
    console.log(result);
});
var http = require('http');

    module.exports = {
        'sendResults': function(callback) {
            http.get(' http://gs-class.com/nodejs/students.php', function(res) {
                var whole = '';
                res.on('data', function(chunk) {
                    whole += chunk;
                });
                res.on('end', function() {
                    callback(whole)
                });
            });
        }
    }

您需要使用异步方法(回调、承诺)来解决问题

可能重复您从回调返回。正在工作!!!Thnx。我是node.js的新手,所以我必须研究你的代码better@UlukbekAbylbekov在编写Node.js之前,您应该具有丰富的异步编程知识。无论如何如果这对你有效,请接受答案。