打印机.js未打印到控制台

Printer.js not printing to console

本文关键字:控制台 打印 js 打印机      更新时间:2023-09-26

因此,出于某种原因,在我将打印函数移动到他们自己的文件并尝试在我的应用程序上运行 node 命令后,突然.js文件,它不想在我的控制台中打印任何内容。为什么?

我仍在努力调试这个,因为当我运行以下命令时没有打印出任何东西。

node app.js myusernamehere

有人知道吗?

应用.js

var profile = require("./profile.js");
var users = process.argv.slice(2);
users.forEach(profile.get);

简介.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;

打印机.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;

我不知道实际发生了什么,但它现在可以工作了,我的代码中没有任何更改。