我想使用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");
});
});
答案 0 :(得分: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");
});
});
file_path": <folder_path>,
文件夹路径而不是文件路径。它可能会起作用。