Github calls only execute once in a for loop
Closed this issue · 1 comments
For some reason, even though data.length is 2 or more, createFile only executes once.
for(var i = 0; i < data.length; i++){
github.repos.createFile({
user: "vydingding",
repo: "vydingding.github.io",
path: "_data/" + facultyArray[i] + ".html",
message: "Individual faculty",
content: content
}, function(err, res) {
console.log(err, res);
});
}
And I'm getting a 409 that says refs/head/master is at random-hash but expected another-random-hash. Sometimes it creates the first file, sometimes it creates the last file.
I also tried doing two github.repos.createFiles in a row, and it pops the same error code 409, status: conflict.
Is there a way around this?
I think the issue is that you're calling an async function in a for loop. If data.length is 2 for example, it calls createFile for i=0, and then for i=1 before the i=0 call is necessarily complete. From github api doc (https://developer.github.com/v3/git/):
Git DB API functions will return a 409 Conflict if the git repository for a Repository is empty or unavailable. This typically means it is being created still. Contact Support if this response status persists.
As a test, call createFile for i=0 (hardcoded), and then in the completion handler, call createFile for i=1 (hardcoded).