2017-07-31 4 views
0

私はグローバル変数なぜ最初の配列項目が未定義になるのですか?

var URL = []; 

を設定し、データ取得:

casper.then(function() { 
    // I get 19 links from this code 
    URL = this.evaluate(getURL); 
    // I can see i get the first link 
    console.log(URL[0] + '***************'); 
}); 

が、この関数の後に私のURLは、[0]未定義示しています。

// But this URL[0] shows the error that 
casper.thenOpen("'" + URL[0] + "'", function() { 
    this.echo(this.getTitle()); 
}); 

このようなエラー:

[debug] [phantom] opening url: 'undefined', HTTP GET 
[debug] [phantom] Navigation requested: url=file:///Users/motogod19/PhantomPractice/parse_movie/'undefined', type=Other, willNavigate=true, isMainFrame=true 
[warning] [phantom] Loading resource failed with status=fail: file:///Users/motogod19/PhantomPractice/parse_movie/'undefined' 

なぜ?私はそれを理解することはできません。何か案は ?前もって感謝します。

+0

あなたの 'evaluate'はおそらく何も返しません。 – Vaviloff

+0

それは確かにデータがあります。私は解決策、返信のおかげで見つける。 –

答えて

1

asyncメソッドを呼び出します。したがって、この変数の値を必要とする次のメソッドを呼び出す前に、まずURLの値を取得する必要があります。これを試してください:

casper.then(function() { 
    // I get 19 links from this code 
    URL = this.evaluate(getURL); 
    // I can see i get the first link 
    console.log(URL[0] + '***************'); 

    if (!URL[0]) { 
    // should handle the condition where url list is empty 
    return; 
    } 

    casper.thenOpen("'" + URL[0] + "'", function() { 
    this.echo(this.getTitle()); 
    }); 
}); 
+0

おかげさまで未定義の問題を解決します。 –

関連する問題