【问题标题】:Printer.js not printing to console打印机。没有打印到控制台
【发布时间】:2016-02-29 22:55:25
【问题描述】:

因此,由于某种原因,在我将打印功能移动到他们自己的文件并尝试在我的 app.js 文件上运行 node 命令之后,它突然不想在我的控制台中打印任何内容。这是为什么呢?

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

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;

【问题讨论】:

  • 有人知道为什么什么都没有打印出来吗?

标签: javascript node.js


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 2017-12-18
    • 2016-11-26
    • 2012-02-07
    • 2015-05-04
    • 2016-11-19
    • 2018-07-14
    相关资源
    最近更新 更多