Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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_Node.js_Recursion_Github Api - Fatal编程技术网

递归javascript函数返回顺序不正确?

递归javascript函数返回顺序不正确?,javascript,node.js,recursion,github-api,Javascript,Node.js,Recursion,Github Api,因此,我有下面的代码尝试在Github repo中递归查找文件 文件夹结构如下所示 大师: 硕士学位:gpapi/ 硕士:文件/ 母版:文档/授权/ 硕士:文件/下载/ 硕士:文件/搜索/ 我希望输出如下所示: (master:call)->master:Documentation/是一棵树 (master:Documentation/call)->master:Documentation/auth/是一棵树 (master:Documentation/auth/call)->master:Do

因此,我有下面的代码尝试在Github repo中递归查找文件

文件夹结构如下所示

大师:

硕士学位:gpapi/

硕士:文件/

母版:文档/授权/

硕士:文件/下载/

硕士:文件/搜索/

我希望输出如下所示:

(master:call)->master:Documentation/是一棵树

(master:Documentation/call)->master:Documentation/auth/是一棵树

(master:Documentation/auth/call)->master:Documentation/auth/contents

(master:Documentation/call)->master:Documentation/download/是一棵树

(主控:文档/下载/调用)->主控:文档/下载/内容

(master:Documentation/call)->master:Documentation/search/是一棵树

(主控:文档/搜索/调用)->主控:文档/搜索/内容

(主控:文档/呼叫)->主控:文档/内容

(master:call)->master:gpapi/是一棵树

(master:gpapi/call)->master:gpapi/contents

(主机:呼叫)->主机:目录

相反,输出如下所示:

(master:call)->master:Documentation/是一棵树

(master:call)->master:gpapi/是一棵树

(主机:呼叫)->主机:目录

(master:Documentation/call)->master:Documentation/auth/是一棵树

(master:Documentation/call)->master:Documentation/download/是一棵树

(master:Documentation/call)->master:Documentation/search/是一棵树

(主控:文档/呼叫)->主控:文档/内容

(master:gpapi/call)->master:gpapi/contents

(主控:文档/下载/调用)->主控:文档/下载/内容

(主控:文档/搜索/调用)->主控:文档/搜索/内容

(master:Documentation/auth/call)->master:Documentation/auth/contents

在最近的调用可以评估文档的子目录/树之前,第一个函数调用如何评估gpapi目录/树?这是我如何执行递归的问题,还是控制台日志工作的问题

这是密码

//Package to connect to GitHub's GraphQL API
var GithubGraphQLApi = require('node-github-graphql') 

//Package to edit SQL db
var sql = require('mysql'); 

//Connect to GitHub API
var github = new GithubGraphQLApi({
  token: process.env.GITHUB_API_TOKEN,
  debug: true
})

//Call the recursive function
populatefiles("googleplay-api", "master:");

//Find all the files in a repo for a given path
function populatefiles(repo_name, path){
    //Query the GitHub API for all files in repo <repo_name>
    //found at <path>
    github.query(`
        {
            viewer {
                repository(name: "` + repo_name +`") {
                    object(expression: "` + path +`") {
                        ... on Tree{
                            entries{
                                name
                                type
                                mode
                            }
                        }
                    }
                }
            }
        }

        `, null, (res, err) => {

        //The array of files returned by Github
        var entries  = res.data.viewer.repository.object.entries

        for (var e in entries){
            var entry = entries[e];

            //If the entry is a directory("tree") 
            if(entry.type === "tree"){

                //update the path and log it
                var newpath = path + entry.name + "/";
                console.log(newpath + " is a tree!");

                //call the function with the new path
                populatefiles(repo_name, newpath);
            }
        }
        //log all entries found at <path>
        console.log(path + " contents");
        //console.log(JSON.stringify(res, null, 2))
    })
}
//连接到GitHub的GraphQL API的包
var GithubGraphQLApi=require('node-github-graphql')
//用于编辑SQL数据库的包
var sql=require('mysql');
//连接到GitHub API
var github=新的GithubGraphQLApi({
令牌:process.env.GITHUB\u API\u令牌,
调试:正确
})
//调用递归函数
PopulateFile(“GooglePlayAPI”,“master:”);
//查找给定路径的repo中的所有文件
函数populateFile(repo_名称、路径){
//在GitHub API中查询repo中的所有文件
//发现于
github.query(`
{
观众{
存储库(名称:“`+repo_名称+`”){
对象(表达式:“`+path+`”){
…在树上{
条目{
名称
类型
模式
}
}
}
}
}
}
`,null,(res,err)=>{
//Github返回的文件数组
var entries=res.data.viewer.repository.object.entries
for(分录中的var e){
var分录=分录[e];
//如果条目是目录(“树”)
if(entry.type==“tree”){
//更新路径并记录它
var newpath=path+entry.name+“/”;
log(newpath+“是一棵树!”);
//使用新路径调用函数
populatefiles(repo_name,newpath);
}
}
//记录在上找到的所有条目
console.log(路径+“内容”);
//log(JSON.stringify(res,null,2))
})
}

您正在处理承诺,而承诺是异步的。要使它们同步工作,必须使用
.then()
或类似的方法将它们“链接”在一起

我还没有测试过这段代码,但这是总体思路。您需要分配
github.query()
的返回值,这将是承诺。然后可以使用
.Then()
。在
for
循环中,事情变得有点有趣

我在这里的做法是(将所有承诺放入一个数组,然后调用一个
承诺。all
,您无法确定
for
循环每次迭代的顺序。如果您需要确定顺序,您还需要将它们链接起来。我在注释中插入了该循环的示例代码。(请参见
pr2
行)

//调用递归函数
PopulateFile(“谷歌游戏api”,“主控:”)
。然后(x=>console.log('all done');
.catch(e=>console.log('error!',e));
//查找给定路径的repo中的所有文件
函数populateFile(repo_名称、路径){
//在GitHub API中查询repo中的所有文件
//发现于
var pr=github.query(`
{
观众{
存储库(名称:“`+repo_名称+`”){
对象(表达式:“`+path+`”){
…在树上{
条目{
名称
类型
模式
}
}
}
}
}
}
`);
pr=pr.then(res=>{
//Github返回的文件数组
var entries=res.data.viewer.repository.object.entries
var-prarr=[];
//var pr2=Promise.resolve();//如果您担心
//Call the recursive function
populatefiles("googleplay-api", "master:")
    .then(x => console.log('all done'));
.catch(e => console.log('error!', e));

//Find all the files in a repo for a given path
function populatefiles(repo_name, path) {
    //Query the GitHub API for all files in repo <repo_name>
    //found at <path>
    var pr = github.query(`
        {
            viewer {
                repository(name: "` + repo_name + `") {
                    object(expression: "` + path + `") {
                        ... on Tree{
                            entries{
                                name
                                type
                                mode
                            }
                        }
                    }
                }
            }
        }

        `);

    pr = pr.then(res => {

        //The array of files returned by Github
        var entries = res.data.viewer.repository.object.entries
        var prarr = [];
        // var pr2 = Promise.resolve(); // create a resolved promise to start the chain, if you're worried about order of the for loop.
        for (var e in entries) {
            var entry = entries[e];

            //If the entry is a directory("tree") 
            if (entry.type === "tree") {

                //update the path and log it
                var newpath = path + entry.name + "/";
                console.log(newpath + " is a tree!");

                //call the function with the new path
                prarr.push(populatefiles(repo_name, newpath));
                // pr2 = pr2.then(populatefiles(repo_name, newpath)); // if you're worried about order of the loop
            }
        }
        //log all entries found at <path>
        console.log(path + " contents");
        //console.log(JSON.stringify(res, null, 2))
        return Promise.all(prarr);
        //return pr2; // if you're worried about the order of the loop
    });

    return pr;
}