无法通过Node.js向Git Hub发送多个文件

时间:2019-06-05 10:59:22

标签: node.js git github-api

我正在尝试通过节点js中的node-rest-client将更多文件发送到github。我正在为每个文件调用promise,每个promise将调用一个http客户端将文件发送到github。但是我看到只有两个文件正在发送,而其他文件没有任何状态。

通过诺言发送多文件的代码

return await Promise.all(dataToSend.map(file => {
                    return new Promise((resolve, reject) => {
                        let fileName = repoName+"/" +file.filename;
                        let fileData = file.filedata;
                        let contenttype = file.contenttype;

                        salesforceToGit.sendDataToGIt(fileName, fileData, contenttype, commitMsg, "metadata", function (data, response) {
                            resolve(data);
                        });
                    });

                }));

和用于将文件发送到GIT集线器的API

function sendDataToGIt(filename, filedata, contenttype, commitMsg, repoName, callback) {
    var buff = new Buffer.from(filedata);
    var base64data = buff.toString('base64');
    contenttype = "application/json";
    var dataObj = {
        "message": commitMsg,
        "content": base64data
    };
    var agrs = {
        data: dataObj,
        headers: {
            'Content-Type': contenttype,
            'Authorization': '******************',
            'User-Agent': 'adadasd'
        }
    };
    try{
    let gitHubUrl = "https://api.github.com/repos/REPO/" + repoName + "/contents/" + filename;
    console.log("sending api request URL : " + gitHubUrl);
    client.put(gitHubUrl,agrs,function(data,response){
        if(response.statusCode === 201 || response.statusCode === 200){
            console.log("file created in Git : " + data.content.path);
            callback(data,response);
        }else{
            console.log("Error occured : " + filename);
        }
    });

}catch(err){
    throw new Error('Error in sendDataToGIt : ' + err);
}

}

0 个答案:

没有答案