私は数日だけNodeを学習しました。私がNodeに切り替えることを決めた理由は、それがサーバー側の言語として使用していたphpよりも高速です。最近、私は、PHPを使うことは、リクエストを送信し、再帰的にデータを処理することに関しては効果がないことを認識しましたmy previous post considering php and VK api。 (ところで、私はその解決策を見つけました)ノードjs:再帰関数を順番に実行/制御フロー
私の関数をNodeに書き直しました。ノードの非同期性のため正しく機能しません。この機能は、ソーシャルネットワークのユーザー間のすべての間接的な接続を検索し、見つかった各間接的な接続に関するデータを接続にを配列を埋める必要があります(また、それぞれについてのデータの文字列を含む配列である:短いそれを置くために
(すなわち、name、second_name、id))。
var find_links = function(n, node1, ports,
vk, visited, connection, connections, event_count, callback) {
n+=1;
if(n<2) { // I don't want to go too far with my recursion. I keep track of it
// vk.request just send a request to api and gets a list of friends of a user with user_id: node1
vk.request('friends.get', {'user_id' : node1, 'fields' : 'domain, first_name'}, function(_o) {
// when we get response it may be either response or 'error' if user has deleted their account
if('response' in _o) { // just checking if we have response
for (var item of _o['response']['items']) {
for (var port of ports['response']['items']) {
if(port['id'] == item['id']) {
data = item['first_name'] + ' ' + item['last_name'] + ' ' + item['id']; // info about user
connection.push(data);
connections.push(connection);
connection.pop();
break;
}
else {
if (!visited.present(item['id'])) { // if we haven't encountered this user before
data = item['first_name'] + ' ' + item['last_name'] + ' ' + item['id']; // info about user
connection.push(data);
visited.push(item['id']); // we add user_id to visited array
// recursion
find_links(n, item['id'], ports, vk, visited, connection, connections, event_count, console.log);
connection.pop();
}
}
}
}
if (n == 1) {
callback("conn is "+connections);
}
}
else {
return 1;
}
});
}
else { // if n == 2, i check only friends of current user and return
vk.request('friends.get', {'user_id' : node1, 'fields' : 'domain, first_name'}, function(_o) {
if('response' in _o) {
for(item_ of _o['response']['items']) {
for(var arg_ of ports['response']['items']) {
if (arg_['id'] === item_['id'] && !visited.present(item_['id'])) {
data = item_['first_name'] + ' ' + item_['last_name'] + ' ' + item_['id'];
connection.push(data); // add data about user to connection
connections.push(connection);
connection.pop();
break;
}
else {
connection.pop();
}
}
}
}
return 1;
})
}
}
私は接続見つかったすべてのチェーンについてのデータが含まれている必要があり配列をログインしたときにそう、私はこの,,,,,,,,,,,,,,,,
を取得します。アイテムは配列にプッシュされません。
したがって、コードをループ内で順番に実行し、処理を続行する前に再帰関数が終了するのを待つにはどうすればよいですか?私は友人を要求したときVKから
応答は以下のようになります。あなたが必要なもの
{ response:
{ count: 67,
items:
[ { id: 6248811,
first_name: 'Екатерина',
last_name: 'Федорова',
domain: 'k.fedorova' },
{ id: 8102967,
first_name: 'Роман',
last_name: 'Соколинский',
domain: 'majestyk',
hidden: 1 },
.....
]
}
}
このコードは少し混乱しています...削除されたバージョンを提供できますか?データを直接押すだけで、なぜ押してポップするのですか?しかし、ブロックスコープ変数(myvar = something)を使用して見ることができます。それらは外部スコープの影響を受けず、非同期ループで非常に便利です。そして、コールバックは実際にあなたが最近やっていることではありません(約束を見てください、例えばhttps://github.com/kriskowal/q) – Rienk
@Rienkデータを直接プッシュするとどういう意味ですか?私はプッシュし、配列からデータをポップする必要があります。この方法で、私が訪れたユーザーを追跡することができるからです。今のところ、私はこれを可能な限り最良の方法と見ています。 –
これはちょっと混乱します。訪問ユーザーのためにオブジェクトを作成しないのはなぜですか? => var connections = {}; connections [userid] =いくつかの接続データ。そのあとで、既に訪問したかどうかを確認する。if(接続の種類[otheruserid] === '未定義'){connections [otheruserid] =接続データ;} else {continue;}; – Rienk