отправить несколько файлов в репозиторий git - PullRequest
0 голосов
/ 16 октября 2018

Я хочу отправить несколько файлов в репозиторий git, используя nodejs.только один движущийся файл.при зацикливании его ошибка выдачи типа Не удается установить заголовки после того, как они отправлены клиенту.ниже мой кодподскажите, где я ошибся.

    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");

        });

    });

1 Ответ

0 голосов
/ 16 октября 2018

Я никогда не использовал это раньше.Похоже, что вы сделали, установили весь заголовок и другие вещи, а затем отправили сообщение.и сделай эти вещи снова.Но, согласно вашей ошибке, похоже, что вы не смогли сбросить заголовок.

Есть два предложения: 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");

            });

        });
отправить все файлы одновременно.попробуйте использовать file_path": <folder_path>, путь к папке вместо пути к файлу.это может сработать.
...