简体   繁体   中英

Printer.js not printing to console

So for some reason all of a sudden after I move my print functions to their own file and try to run the node command on my app.js file then it doesn't want to print anything in my console. Why is that?

I am still struggling with debugging this one as nothing is printing out when I run the following command.

node app.js myusernamehere

Anyone have any idea?

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;

我不知道实际发生了什么,但现在可以正常运行,代码中的任何内容都没有更改。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM