0
因此,出於某種原因,在將我的打印功能移動到他們自己的文件並嘗試在我的app.js文件上運行節點命令後,突然間它不想打印任何東西在我的控制檯。這是爲什麼?Printer.js不打印到控制檯
我仍在努力調試這一個,因爲當我運行以下命令時沒有打印出任何內容。
node app.js myusernamehere
任何人有什麼想法?
app.js
var profile = require("./profile.js");
var users = process.argv.slice(2);
users.forEach(profile.get);
profile.js
//Problem: We need a simple way to look at a user's badge count and Javascript points
//Solution: Use Node.js to connect to Treehouse's API to get profile information to print out
var https = require("https");
var http = require("http");
var printer = require("./printer.js");
function get(username) {
//Connect to API URL (http://teamtreehouse.com/username.json)
var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
var body = "";
//Read the data
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
if (response.statusCode == 200) {
try {
//Parse the data
var profile = JSON.parse(body);
//Print the data
printer.printMessage(username, profile.badges.length, profile.points.JavaScript);
} catch(error) {
//Parse Error
printer.printError(error);
}
} else {
//Status Code Error
printer.printError({message: "There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"});
}
});
});
//Connection Error
request.on('error', printer.printError);
}
module.exports.get = get;
printer.js
//Print out message
function printMessage(username, badgeCount, points) {
var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in Javascript";
console.log(message);
}
//Print out error messages
function printError(error) {
console.error(error.message);
}
module.exports.printMessage = printMessage;
module.exports.printError = printError;
任何人有任何想法,爲什麼沒有打印出來? – user3732216