Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 链接承诺导致未定义_Javascript_Jquery_Angularjs_Promise_Jquery Deferred - Fatal编程技术网

Javascript 链接承诺导致未定义

Javascript 链接承诺导致未定义,javascript,jquery,angularjs,promise,jquery-deferred,Javascript,Jquery,Angularjs,Promise,Jquery Deferred,我在这个函数中不断遇到未定义的错误。它被分解为5个单独的函数,我尝试在前一个函数完成后运行每个函数,但使用前一个函数返回的数据。我可以嵌套所有回调,但我宁愿使用承诺 var checkedOutReport = function () { var rootSite = window.location.protocol + "//" + window.location.hostname; //Create array with each collection site

我在这个函数中不断遇到未定义的错误。它被分解为5个单独的函数,我尝试在前一个函数完成后运行每个函数,但使用前一个函数返回的数据。我可以嵌套所有回调,但我宁愿使用承诺

var checkedOutReport = function () { 
    var rootSite = window.location.protocol + "//" + window.location.hostname;

    //Create array with each collection site
    function getAllSites (){
        var d = $q.defer();
        var allSites = [];

        $().SPServices({
            operation: "GetAllSubWebCollection",
            completefunc: function (xData,Status){
                var result = $(xData.responseXML);

                result.find('Web').each(function (){
                    var self = $(this);
                    allSites.push({
                        siteTitle: self.attr('Title'),
                        siteUrl: self.attr('Url')   
                    })
                })  
            }
        })
        d.resolve(allSites)
        return d.promise;
    }
    //Get libraries for each siteUrl
    function getSiteLibraries (allSites) {
        var siteCount = allSites.length;
        var listPromise = [];

        //Go through each site and find document libraries 
        for (var i = 0; i < siteCount; i++){
            listPromise[i] = $().SPServices({
                operation: "GetListCollection",
                webURL: allSites[i].siteUrl
            })
        }

        //Promise array contains all lists for each site
        //Get libraries for each site 
        $q.all(listPromise).then(findLibraries(listPromise))
    }
    function findLibraries (promises){
        var d = $q.defer();
        var siteCount = promises.length;
        var allLibraries = [];
        //For each site, find the document libraries
        for (var i = 0; i < siteCount; i++){
            $(promises[i].responseXML).find("List[ServerTemplate='101']").each(function(){
                var self = $(this);
                allLibraries.push({
                    listName = self.attr('Title'),
                    listId = self.attr('ID'),
                    siteUrl = allSites[i].siteUrl
                })
            })
        }
        console.log(allLibraries);
        d.resolve(allLibraries);
        return d.promise;
    }

    //CAML QUERIES
    var cQueryOptions = "<QueryOptions><ViewAttributes Scope='RecursiveAll' IncludeRootFolder='True' /></QueryOptions>",
        cQueryAllCheckedOutDocuments = '<Query><Where><IsNotNull><FieldRef Name="CheckoutUser" /></IsNotNull></Where></Query>',
        cQueryModifiedBySystemAccount = '<Query><Where><Contains><FieldRef Name="Editor" /><Value Type="User">system</Value></Contains></Where></Query>',
        cViewFields = "<ViewFields Properties='True' />";

    //For each library in site collection, find documents which meet criteria 
    function searchLibraries (library) {
        var libraryCount = library.length;
        var itemPromise = [];

        for (var i = 0; i < libraryCount; i++){
            itemPromise[i] = $().SPServices({
                operation: "GetListItems",
                webURL: library[i].siteUrl,
                listName: library[i].listID,
                CAMLViewFields: cViewFields,
                CAMLQuery: cQueryAllCheckedOutDocuments
            })
        }

        $q.all(itemPromise).then(parseSearchResult(itemPromise))
    }
    //Clean up result
    function parseSearchResult(result){
        var d = $q.defer()
        var resultCount = result.length; 
        var returnedFiles = [];
        for(var i = 0; i < resultCount; i++){
            $(result[i].responseXML).SPFilterNode("z:row").each(function (){
                var self = $(this);

                var fileName = self.attr('ows_LinkFilename');
                var fileUrl = self.attr('ows_FileDirRef').split("#");
                var checkedTo = self.attr('ows_LinkCheckedOutTitle');
                var modified = self.attr('ows_Modified');

                returnedFiles.push({
                    fileName: fileName,
                    fileUrl: fileUrl[1],
                    checkedTo: checkedTo,
                    modified: modified
                });
            })
        }
        d.resolve(returnedFiles);
        return d.promise;
    }

    getAllSites()
        .then(
            getSiteLibraries(allSites)
            )
        .then(
            searchLibraries(allLibraries)
            );
}
var checkedOutReport=function(){
var rootSite=window.location.protocol+“/”+window.location.hostname;
//使用每个集合站点创建阵列
函数getAllSites(){
var d=$q.defer();
var-allSites=[];
$().SPServices({
操作:“GetAllSubWebCollection”,
completefunc:函数(扩展数据、状态){
var result=$(扩展数据.responseXML);
result.find('Web')。每个(函数(){
var self=$(这是);
allSites.push({
siteTitle:self.attr('Title'),
siteUrl:self.attr('Url')
})
})  
}
})
d、 解析(所有站点)
回报承诺;
}
//获取每个站点URL的库
函数getSiteLibraries(所有站点){
var sitecont=allSites.length;
var listPromise=[];
//浏览每个站点并查找文档库
对于(变量i=0;i
这里有很多问题。我会尽我所能描述尽可能多的。可能还有几个

d.resolve(所有站点)
需要在
completefunc
回调中,因为在填充
allSites
之前,您不想解析承诺

getSiteLibraries()
searchLibraries()
需要兑现承诺

var checkedOutReport = function () { 
    var rootSite = window.location.protocol + "//" + window.location.hostname;

    //Create array with each collection site
    function getAllSites (){
        var d = $q.defer();
        var allSites = [];

        $().SPServices({
            operation: "GetAllSubWebCollection",
            completefunc: function (xData,Status){
                var result = $(xData.responseXML);

                result.find('Web').each(function (){
                    var self = $(this);
                    allSites.push({
                        siteTitle: self.attr('Title'),
                        siteUrl: self.attr('Url')   
                    })
                })  
            }
        })
        d.resolve(allSites)
        return d.promise;
    }
    //Get libraries for each siteUrl
    function getSiteLibraries (allSites) {
        var siteCount = allSites.length;
        var listPromise = [];

        //Go through each site and find document libraries 
        for (var i = 0; i < siteCount; i++){
            listPromise[i] = $().SPServices({
                operation: "GetListCollection",
                webURL: allSites[i].siteUrl
            })
        }

        //Promise array contains all lists for each site
        //Get libraries for each site 
        $q.all(listPromise).then(findLibraries(listPromise))
    }
    function findLibraries (promises){
        var d = $q.defer();
        var siteCount = promises.length;
        var allLibraries = [];
        //For each site, find the document libraries
        for (var i = 0; i < siteCount; i++){
            $(promises[i].responseXML).find("List[ServerTemplate='101']").each(function(){
                var self = $(this);
                allLibraries.push({
                    listName = self.attr('Title'),
                    listId = self.attr('ID'),
                    siteUrl = allSites[i].siteUrl
                })
            })
        }
        console.log(allLibraries);
        d.resolve(allLibraries);
        return d.promise;
    }

    //CAML QUERIES
    var cQueryOptions = "<QueryOptions><ViewAttributes Scope='RecursiveAll' IncludeRootFolder='True' /></QueryOptions>",
        cQueryAllCheckedOutDocuments = '<Query><Where><IsNotNull><FieldRef Name="CheckoutUser" /></IsNotNull></Where></Query>',
        cQueryModifiedBySystemAccount = '<Query><Where><Contains><FieldRef Name="Editor" /><Value Type="User">system</Value></Contains></Where></Query>',
        cViewFields = "<ViewFields Properties='True' />";

    //For each library in site collection, find documents which meet criteria 
    function searchLibraries (library) {
        var libraryCount = library.length;
        var itemPromise = [];

        for (var i = 0; i < libraryCount; i++){
            itemPromise[i] = $().SPServices({
                operation: "GetListItems",
                webURL: library[i].siteUrl,
                listName: library[i].listID,
                CAMLViewFields: cViewFields,
                CAMLQuery: cQueryAllCheckedOutDocuments
            })
        }

        $q.all(itemPromise).then(parseSearchResult(itemPromise))
    }
    //Clean up result
    function parseSearchResult(result){
        var d = $q.defer()
        var resultCount = result.length; 
        var returnedFiles = [];
        for(var i = 0; i < resultCount; i++){
            $(result[i].responseXML).SPFilterNode("z:row").each(function (){
                var self = $(this);

                var fileName = self.attr('ows_LinkFilename');
                var fileUrl = self.attr('ows_FileDirRef').split("#");
                var checkedTo = self.attr('ows_LinkCheckedOutTitle');
                var modified = self.attr('ows_Modified');

                returnedFiles.push({
                    fileName: fileName,
                    fileUrl: fileUrl[1],
                    checkedTo: checkedTo,
                    modified: modified
                });
            })
        }
        d.resolve(returnedFiles);
        return d.promise;
    }

    getAllSites()
        .then(
            getSiteLibraries(allSites)
            )
        .then(
            searchLibraries(allLibraries)
            );
}
findLibraries()
parsearchresult()
没有任何异步代码,因此不需要返回承诺,只需返回结果即可

$q.all(listPromise)。然后(findLibraries(listPromise))
实际立即执行
findLibraries()
,只需使用
$q.all(listPromise)。然后(findLibraries)
。这同样适用于
$q.all(itemPromise)。然后(parseSearchResult(itemPromise))

这同样适用于

getAllSites()
    .then(
        getSiteLibraries(allSites)
        )
    .then(
        searchLibraries(allLibraries)
        );
改为:

getAllSites()
    .then(
        getSiteLibraries
        )
    .then(
        searchLibraries
        );
应用上述更改后

var checkedOutReport = function () { 
    var rootSite = window.location.protocol + "//" + window.location.hostname;

    //Create array with each collection site
    function getAllSites (){
        var d = $q.defer();
        var allSites = [];

        $().SPServices({
            operation: "GetAllSubWebCollection",
            completefunc: function (xData,Status){
                var result = $(xData.responseXML);

                result.find('Web').each(function (){
                    var self = $(this);
                    allSites.push({
                        siteTitle: self.attr('Title'),
                        siteUrl: self.attr('Url')  
                    })
                })  
                d.resolve(allSites); // resolve only when complete
            }
        });
        return d.promise;
    }
    //Get libraries for each siteUrl
    function getSiteLibraries (allSites) {
        var siteCount = allSites.length;
        var listPromise = [];

        //Go through each site and find document libraries 
        for (var i = 0; i < siteCount; i++){
            listPromise[i] = $().SPServices({
                operation: "GetListCollection",
                webURL: allSites[i].siteUrl
            })
        }

        //Promise array contains all lists for each site
        //Get libraries for each site 
        return $q.all(listPromise).then(findLibraries); // need to return the promise
    }
    function findLibraries (promises){
        var siteCount = promises.length;
        var allLibraries = [];
        //For each site, find the document libraries
        for (var i = 0; i < siteCount; i++){
            $(promises[i].responseXML).find("List[ServerTemplate='101']").each(function(){
                var self = $(this);
                allLibraries.push({
                    listName: self.attr('Title'),
                    listId: self.attr('ID'),
                    siteUrl: allSites[i].siteUrl
                })
            })
        }
        console.log(allLibraries);
        return allLibraries; // don't need a promise here
    }

    //CAML QUERIES
    var cQueryOptions = "<QueryOptions><ViewAttributes Scope='RecursiveAll' IncludeRootFolder='True' /></QueryOptions>",
        cQueryAllCheckedOutDocuments = '<Query><Where><IsNotNull><FieldRef Name="CheckoutUser" /></IsNotNull></Where></Query>',
        cQueryModifiedBySystemAccount = '<Query><Where><Contains><FieldRef Name="Editor" /><Value Type="User">system</Value></Contains></Where></Query>',
        cViewFields = "<ViewFields Properties='True' />";

    //For each library in site collection, find documents which meet criteria 
    function searchLibraries (library) {
        var libraryCount = library.length;
        var itemPromise = [];

        for (var i = 0; i < libraryCount; i++){
            itemPromise[i] = $().SPServices({
                operation: "GetListItems",
                webURL: library[i].siteUrl,
                listName: library[i].listID,
                CAMLViewFields: cViewFields,
                CAMLQuery: cQueryAllCheckedOutDocuments
            })
        }

        return $q.all(itemPromise).then(parseSearchResult); // need to return promise
    }
    //Clean up result
    function parseSearchResult(result){
        var resultCount = result.length; 
        var returnedFiles = [];
        for(var i = 0; i < resultCount; i++){
            $(result[i].responseXML).SPFilterNode("z:row").each(function (){
                var self = $(this);

                var fileName = self.attr('ows_LinkFilename');
                var fileUrl = self.attr('ows_FileDirRef').split("#");
                var checkedTo = self.attr('ows_LinkCheckedOutTitle');
                var modified = self.attr('ows_Modified');

                returnedFiles.push({
                    fileName: fileName,
                    fileUrl: fileUrl[1],
                    checkedTo: checkedTo,
                    modified: modified
                });
            })
        }
        return returnedFiles; // don't need a promise here
    }

    getAllSites()
        .then(
            getSiteLibraries
            )
        .then(
            searchLibraries
            );
}
var checkedOutReport=function(){
var rootSite=window.location.protocol+“/”+window.location.hostname;
//使用每个集合站点创建阵列
函数getAllSites(){
var d=$q.defer();
var-allSites=[];
$().SPServices({
操作:“GetAllSubWebCollection”,
completefunc:函数(扩展数据、状态){
var result=$(扩展数据.responseXML);
result.find('Web')。每个(函数(){
var self=$(这是);
allSites.push({
siteTitle:self.attr('Title'),
siteUrl:self.attr('Url')
})
})  
d、 解析(所有站点);//仅在完成时解析
}
});
返回d.pro