简体   繁体   English

node_klout node.js:紧跟着getUserScore的getKloutIdentity无法正常工作

[英]node_klout node.js : getKloutIdentity followed by getUserScore not working

I have a very simple code and the first console.log prints the klout_user.id, but it never prints the second one, ie, klout_response.score. 我有一个非常简单的代码,第一个console.log打印klout_user.id,但从不打印第二个控制台,即klout_response.score。 names is an array of twitter screen names. 名称是Twitter屏幕名称的数组。 The problem could be as well because of the control flow. 由于控制流程的缘故,问题也可能出现。 I tried running both the klout calls independently and it works. 我试过独立运行两个klout调用,它可以工作。 But it does not work in the following code. 但是在以下代码中不起作用。 Any clue ? 有什么线索吗?

names.forEach(function (name, i) { 
    klout.getKloutIdentity(name, function(error, klout_user) {
        if (klout_user.hasOwnProperty("id") && klout_user.id > 0) {
            console.log("klout user", name, "has id : ", klout_user.id); 
            klout.getUserScore(klout_user.id, function(error, klout_response) {
            console.log("klout_user score : ", klout_response.score);
        });
         }
    });
});

I am of the impression that, since it prints the first console.log, the call to getUserScore also should be executed. 我的印象是,由于它打印了第一个console.log,因此也应该执行对getUserScore的调用。 But it does not. 但事实并非如此。 what's wrong ? 怎么了 ?

I got the answer by Cojohn at node_klout github page. 我在node_klout github页面上得到了Cojohn的答案。 pasting it here as the answer. 将其粘贴在此处作为答案。


Your code is not retrieving a Klout user score because you're relying on I/O bound functions inside of a for loop. 您的代码没有获取Klout用户评分,因为您依赖于for循环内的I / O绑定函数。 Basically, you're firing off the initial calls to the Klout API, which return normally and print to console, and your process is either finishing or the function is returning before it has a chance to execute klout.getUserScore(). 基本上,您要触发对Klout API的初始调用,该调用通常会返回并打印到控制台,并且您的过程可能已经完成或函数正在返回,然后才有机会执行klout.getUserScore()。 Below is an example of code that will always wait for the response before exiting; 下面是一个示例代码,该代码将始终在退出之前等待响应; note that my test hack is not particularly fast or "asynchronous", it only processes one user at a time and is not suitable for large lists of users. 请注意,我的测试hack并不是特别快或“异步”,它一次只能处理一个用户,不适合大量用户。 My names and api_key vars have been omitted, you'll need to supply your own. 我的名字和api_key vars已被省略,您需要提供自己的名字。

var klout = new Klout(api_key, "json", "v2");
var events = require("events");

var e = new events.EventEmitter();

e.on("done", function() {
    process.exit();
});

e.on("next", function(i) {
    if (i >= names.length) {
        e.emit("done");
        return;
    }

    console.log("retrieving kloutid for user %s", names[i]);
    klout.getKloutIdentity(names[i], function(error, klout_user) {
        if (error) {
            console.log(error);
            e.emit("next", i+1);
            return;
        }

        if (!klout_user.hasOwnProperty("id") || klout_user.id <= 0) {
            e.emit("next", i+1);
            return;
        }

        console.log("klout user %s has id : %s", names[i], klout_user.id); 

        klout.getUserScore(klout_user.id, function(error, klout_response) {
            if (error) {
                console.log(error);
                e.emit("next", i+1);
                return;
            }

            console.log("klout_user score : %s", klout_response.score);
            e.emit("next", i+1);
        });         
    });
});

e.emit("next", 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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