Javascript module.exports中的Node.js我的变量定义不足
我的变量“整”未定义 getStudents.jsJavascript 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;
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之前,您应该具有丰富的异步编程知识。无论如何如果这对你有效,请接受答案。