我有以下要分页的功能,我发现GitHub API只通过他们的回购请求推送了30个项目;
getRepos
this.getRepos = function(username, siteAndPassword) {
// Pagination
return new Promise((resolve, reject) => {
axios.get('https://api.github.com/user/repos', {
auth: { username: username, password: siteAndPassword.password }
})
.then((response) => {
resolve(response.data)
})
.catch((error) => {
reject(error)
})
})
}
在我的命令行输入提示函数passwordPrompt
' .then
调用
passwordPrompt(site)
.then((siteAndPassword) => {
// Auth the user with given password (basic HTTP)
return API.GitHub.getRepos(user, siteAndPassword)
})
.catch((error) => {
console.log(error)
process.exit()
})
.then((repos) => { ... /* Do more crazy stuff with the data */ }
这是我的分页尝试,我将per_page
设置为1
,以便我可以测试它,该函数应该记录currentPage八次,但我只是得到一个空白控制台。
this.getRepos = function(username, siteAndPassword) {
// Pagination
return new Promise((resolve, reject) => {
var currentPage = 1
var fullList = []
axios.get('https://api.github.com/user/repos', {
auth: { username: username, password: siteAndPassword.password },
params: { page: currentPage, per_page: 1 }
})
.then((response) => {
if (response.data.length == 0) {
resolve(fullList)
} else {
console.log(currentPage)
currentPage += 1
axios.get('https://api.github.com/user/repos', {
auth: { username: username, password: siteAndPassword.password },
params: { page: currentPage, per_page: 100 }
})
.then((response) => {
response.data.forEach((item) => {
fullList.push(item)
})
})
.catch((error) => {
console.log(error)
})
}
})
.catch((error) => {
reject(error)
})
})
}