1

我想使用 nodejs 将多个文件发送到 git 存储库。仅移动单个文件。当循环它的抛出错误时,例如在将标头发送到客户端后无法设置标头。下面是我的代码。建议我哪里做错了。

    var base64 = require('file-base64');
for (var i in files) {
    console.log('file load: ' + files[i]);
    base64.encode('/opt/zipoutput/' + files[i], function (err, base64String) {
        convertval = base64String;
        var dataObj = {
            "branch": "master",
            "commit_message": "dump message",
            "actions": [
                {
                    "action": "create",
                    "file_path": files[i],
                    "content": convertval,
                    "encoding": "base64"
                }
            ]
        };
        var filesrc  = files;
        var Client = require('node-rest-client').Client;
        var client = new Client()            
        var args = {
            data: dataObj,
            headers: { 'Content-Type': 'application/json', 'PRIVATE-TOKEN': 'xxxxxxxxxxxxxxxx' },
        };
        client.post("https://gitlab.com/api/v4/projects/77/repository/commits", args, function (data, response) {
            fs.rename('/opt/zipoutput/'+files[i], '/opt/sentFiles/'+files[i], function(err) {
                if (err) throw err;
                console.log("args passed:",args);
                 console.log("successfully moved into Sent file dirctory");
            });
            console.log("file send: True");
            res.send("true");

        });

    });
4

1 回答 1

0

我以前从来没有用过这个。您所做的似乎是设置所有标题和其他内容,然后发送帖子。并再次做那些事情。但是根据您的错误,您似乎无法重置标题。

有两个建议:1.尝试在循环之外设置标题。这意味着它只会设置一次。像这样的东西:

var headers = { 'Content-Type': 'application/json', 'PRIVATE-TOKEN': 'xxxxxxxxxxxxxxxx' };
for (var i in files) {
        console.log('file load: ' + files[i]);
        base64.encode('/opt/zipoutput/' + files[i], function (err, base64String) {
            convertval = base64String;
            var dataObj = {
                "branch": "master",
                "commit_message": "dump message",
                "actions": [
                    {
                        "action": "create",
                        "file_path": files[i],
                        "content": convertval,
                        "encoding": "base64"
                    }
                ]
            };
            var filesrc  = files;
            var Client = require('node-rest-client').Client;
            var client = new Client()            
            var args = {
                data: dataObj,
                headers: headers,
            };
            client.post("https://gitlab.com/api/v4/projects/77/repository/commits", args, function (data, response) {
                fs.rename('/opt/zipoutput/'+files[i], '/opt/sentFiles/'+files[i], function(err) {
                    if (err) throw err;
                    console.log("args passed:",args);
                     console.log("successfully moved into Sent file dirctory");
                });
                console.log("file send: True");
                res.send("true");

            });

        });
  1. 同时发送所有文件。尝试使用file_path": <folder_path>,文件夹路径而不是文件路径。它可能会起作用。
于 2018-10-16T20:44:15.127 回答