2016-05-03 13 views
1

以下のコードでは、特定の基準を満たすSharePointリストのレコード数/行数を取得しようとしています。しかし、私は変数の範囲に問題があります。 getListData()にnumRecordsを返すようにするには、どうすれば新しい変数に設定することができますか?以下の構文では、numRecordsはonQuerySucceeded関数の正しい値(console.logで検証)ですが、トップレベル関数では未定義です。JavaScript REST GETでSharePoint変数スコープの混乱

ありがとうございました!

<!DOCTYPE html> 
<head> 
<script> 

$(function() { 
    var numQuals = 0; 

    numQuals = getListData(1); 
    alert(numQuals); 
}); 

function getListData(myFilter) { 

    $.ajax({ 
    url: "https://mysite.sharepoint.com/tools/_api/web/lists/GetByTitle('C&Q Documents')/items?$select=Document_x0020_TypeId&$filter=Document_x0020_TypeId eq " + myFilter, 
    type: "GET", 
    headers: {"accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose"}, 
    success: onQuerySucceeded, 
    error: onQueryFailed 
    }); 

} 

function onQuerySucceeded(data) { 
    var numRecords = 0; 

    $.each(data.d.results, function (key, value) { 
    numRecords++; 
    }); 

    console.log("in onQuerySucceeded: " + numRecords); 
    return numRecords 
} 

function onQueryFailed(sender, args) {console.log("query failed!");} 

</script> 
</head> 
<body></body> 
</html> 

答えて

0

となっています。電話番号を同期する必要があります。つまり、

$.ajax({ 
    async: false, 
    url: "https://mysite.sharepoint.com/tools/_api/web/lists/GetByTitle('C&Q Documents')/items?$select=Document_x0020_TypeId&$filter=Document_x0020_TypeId eq " + myFilter, 
    type: "GET", 
    headers: {"accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose"}, 
    success: onQuerySucceeded, 
    error: onQueryFailed 
}); 

ありがとうございました!