1

I want to send multiple files into git repository using nodejs. its moving single file only. when looping its throwing error like Cannot set headers after they are sent to the client. below is my code. suggest me where I did wrong.

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

        });

    });
Jay
  • 187
  • 2
  • 13

1 Answers1

0

I never use this before. It seems like what you did is setting all the header and other stuff and then sent the post. and do those things again. But according to your error, it looks like you could not reset the header.

There are two suggestions: 1. try to set the header outside of your loop. which means it would only been set once. something like this :

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. send all the files in the same time. try to use file_path": <folder_path>, folder path instead of file path. it might work.
rd6668
  • 991
  • 6
  • 7