问题描述
环顾四周,找不到答案。所有的facebook api页面都被迭代并保存到一个数组中,我试图运行一个履行履行语句的承诺。我知道功能的丑陋,但我要去看看我能承诺多远。
function pageThroughLikes(facebookPostArray){
var testArray = []
return new Promise ,拒绝){
facebookPostArray.forEach(function(array){
array.forEach(function(innerObject){
if('likes'in innerObject){
if('在内部Object.likes中的分页'){
if('next'in innerObject.likes.paging){
nextPage = innerObject.likes.paging.next;
currentPostId = innerObject.id;
currentDataLength = innerObject.likes.data.length;
i = 0;
do {
$ .get(nextPage,function(nextLikePageData){
likeData = {};
likeData.id = currentPostId;
likeData.likes = {};
likeData.likes.data = nextLikePageData.data
likeData.likes.paging = nextLikePageData.paging
console.log(likeData)
testArray.push(likeData);
facebookPostArray.push(testArray);
console.log('push to postArray')
})
i + = 1;
} while(currentDataLength!= 0&& i> 10)
}
}
}
})
});
fulfill();
console.log('paged through likes')
})
}
一旦这个功能完成,我想运行一个'测试'功能,将我的结果数组转换成CSV格式并下载一个CSV文件。
这是我的测试功能:
function test(){
var convertedPostCSV = convertArrayOfObjectsToCSV(postArray);
downloadCSV(convertedPostCSV);
}
这是我的运行顺序的功能:
$(document).ready(function(){
getPostLikes()然后(function(){
pageThroughLikes(postArray)那么(function(){
test();
});
});
});
我正在努力的是我的'test()'函数在新的页面喜欢被添加到我的'facebookPostArray',或者在thThlLikes函数结束之前。希望有人能给我一些建议/指向正确的方向。
编辑
好的,我已经将我的代码重新格式化为@Winter Soldier已经建议,但是我仍然收到一个未捕获的错误, processData
函数。
这是我的代码:
function pageThroughLikes(facebookPostArray){
console.log('paging through likes')
var testArray = []
var promiseList = [];
返回新的Promise(函数(履行,拒绝){
facebookPostArray.forEach(function(array){
array.forEach(function(innerObject){
if(' in innerObject){
if('paging'in innerObject.likes){
if('next'in innerObject.likes.paging){
nextPage = innerObject.likes.paging.next;
currentPostId = innerObject.id;
currentDataLength = innerObject.likes.data.length;
i = 0;
do {
promiseList.push(
$ .ajax({url:nextPage
}))
i + = 1;
} while(currentDataLength!= 0&& i> 10)
}
}
});
return promiseList;
console.log('paged through likes')
})
processData = function(nextLikePag eData){
likeData = {};
likeData.id = currentPostId;
likeData.likes = {};
likeData.likes.data = nextLikePageData.data
likeData.likes.paging = nextLikePageData.paging
console.log(likeData)
testArray.push(likeData);
facebookPostArray.push(testArray);
console.log('push to postArray')
return likeData;
}
$(document).ready(function(){
getPostLikes()。then(function(){
$ .when.apply(pageThroughLikes postArray),这个).done(function(){
var testArray = []
$ .each(arguments,function(k,v){
var dt = processData (v [0]);
testArray.push(dt);
facebookPostArray.push(dt);
});
console.log(testArray)
test(testArray); // OR
test(facebookPostArray);
});
});
});
函数test(postArray){
var convertedPostCSV = convertArrayOfObjectsToCSV(postArray);
downloadCSV(convertedPostCSV);
}
EDIT 2.0
这是我所有的丑陋的完整代码。仅供参考。图表的问题可能是我隐藏在我的代码的其他部分的某个地方,所以潜在的价值可以充分显示...
var facebookKey = config.FACEBOOK_KEY;
// ASSIGN QUERY TO VARIABLE
var likePage = getQueryVariable(likePage);
var sinceDate = getQueryVariable(sinceDate);
var likeArray = [];
var postArray = [];
函数getQueryVariable(variable){
var query = window.location.search.substring(1);
var vars = query.split('&'); (var i = 0; i< vars.length; i ++){
var pair = vars [i] .split('=');
if(pair [0] == variable){
return pair [1];
}
}
alert(查询变量+变量+找不到);
}
console.log(页面搜索:+ likePage);
console.log(Since:+ sinceDate)
//查找FACEBOOK页面的位置从CHOSEN DATE起
$(document).ready(function (){
getPostLikes()。then(function(){
$ .when.apply(pageThroughLikes(postArray),this).done(function(){
var testArray = []
$ .each(arguments,function(k,v){
var dt = processData(v [0]);
testArray.push(dt);
facebookPostArray.push (dt);
});
console.log(testArray)
test(testArray); // OR
test(facebookPostArray);
});
});
});
函数test(postArray){
var convertedPostCSV = convertArrayOfObjectsToCSV(postArray);
downloadCSV(convertedPostCSV);
}
函数getPostLikes(response){
返回新的Promise(函数(履行,拒绝){
$ .get(https://graph.facebook .com / v2.8 /+ likePage +?fields = access_token,posts.since(+ sinceDate +){likes {id}}& access_token =+ facebookKey,function(facebookData){
var likePageId = facebookData.id;
var testPostArray = [];
if('posts'in facebookData){
var nextPage = facebookData.posts.paging.next;
var check = 0;
postArray.push(facebookData.posts.data);
var currentDataLength =
var i = 0
if('paging'in facebookData.posts){
console.log(new page available);
do {
$ .ajax({
async:false ,
type:GET,
url:nextPage,
success:function(nextPageData){
console.log(New Page Accessed: + nextPage)
i ++;
console.log(Page Number:+ i)
testPostArray.push(nextPageData.data);
if('paging'in nextPageData){
nextPage = nextPageData.paging.next;
console.log(下一页分配);
}
currentDataLength = nextPageData.data.length;
console.log(currentDataLength);
}
});
console.log(DATA LENGTH:+ currentDataLength);
} while(currentDataLength> 0);
testPostArray.forEach(function(element){
console.log(element)
postArray.push(element);
fulfill();
});
}
} else {
console.log('错误:从此日期起没有Facebook帖子!)
reject();
}
console.log(postArray)
});
})
};
console.log(下载...)
函数pageThroughLikes(facebookPostArray){
console.log('paging through likes')
var testArray = []
var promiseList = [];
facebookPostArray.forEach(function(array){
array.forEach(function(innerObject){
if('likes'in innerObject){
if('paging'in innerObject .likes){
if('next'in innerObject.likes.paging){
nextPage = innerObject.likes.paging.next;
currentPostId = innerObject.id;
currentDataLength = innerObject.likes.data.length;
i = 0;
do {
promiseList.push(
$ .ajax({url:nextPage
}))
i + = 1;
} while(currentDataLength!= 0&& i> 10)
}
}
}
})
});
console.log('paged through likes')
return promiseList;
}
processData = function(nextLikePageData){
likeData = {};
likeData.id = currentPostId;
likeData.likes = {};
likeData.likes.data = nextLikePageData.data
likeData.likes.paging = nextLikePageData.paging
console.log(likeData)
testArray.push(likeData);
facebookPostArray.push(testArray);
console.log('push to postArray')
return likeData;
}
//自动下载CSV文件
函数downloadCSV(args){
var data,filename,link;
var csv = convertArrayOfObjectsToCSV(postArray);
if(csv == null)return;
filename = args.filename || export.csv;
if(!csv.match(/ ^ data:text\ / csv / i)){
csv ='data:text / csv; charset = utf-8,'+ CSV;
}
data = encodeURI(csv);
link = document.createElement('a');
link.setAttribute('href',data);
link.setAttribute('download',filename);
link.click();
}
// CONVERT FACEBOOK POSTS OBJECTS TO CSV FORMAT
函数convertArrayOfObjectsToCSV(args){
var result,ctr,keys,columnDelimiter,lineDelimiter,data;
data = args ||空值;
if(data == null ||!data.length){
return null;
}
columnDelimiter = args.columnDelimiter || ,;
lineDelimiter = args.lineDelimiter || \\\
;
keys = Object.keys(data [0]);
result ='';
result + =user_id+ columnDelimiter +post_id+ columnDelimiter +page_id;
result + = lineDelimiter;
data.forEach(function(item){
item.forEach(function(post){
if('likes'in post){
var likeArray = post.likes
likeArray.data.forEach(function(like){
result + = like.id + columnDelimiter + post.id.split('_')。reverse()+ lineDelimiter;
$)
} else {
result + = columnDelimiter + post.id.split('_')。reverse()+ lineDelimiter;
};
}) ;
});
console.log('convert to CSV')
return result;
}
编辑3.0
功能几乎是固定的,只是问题是它不循环。一次完美运行!
function pageThroughLikes(facebookPostArray){
console.log('paging through likes)
var testArray = []
var promiseList = [];
//返回新的Promise(功能(履行,拒绝){
facebookPostArray.forEach(function(array){
array.forEach(function(innerObject)){
if('喜欢'in innerObject){
if('paging'in innerObject.likes){
if('next'in innerObject.likes.paging){
nextPage = innerObject.likes.paging。 next;
currentPostId = innerObject.id;
currentDataLength = innerObject.likes.data.length;
i = 0;
do {
promiseList.push(
$ .ajax({url:nextPage
})然后(function(data,b,promise){
data.id = currentPostId;
if('paging'in data){
if('next'in data.paging){
nextPage = data.paging.next;
}
console.log(nextPage)
return promise;
}))
i + = 1;
console.log(i)
} while(currentDataLength!= 0&& i> 10)
}
}
}
} )
});
console.log('paged through likes')
return promiseList;
}
编辑4.0
这是我现在的代码...
几乎所有的东西终于可以工作,设法得到一堆深夜谷歌搜索的信息。
var facebookKey = config.FACEBOOK_KEY;
// ASSIGN QUERY TO VARIABLE
var likePage = getQueryVariable(likePage);
var sinceDate = getQueryVariable(sinceDate);
var likeArray = [];
var postArray = [];
函数getQueryVariable(variable){
var query = window.location.search.substring(1);
var vars = query.split('&'); (var i = 0; i< vars.length; i ++){
var pair = vars [i] .split('=');
if(pair [0] == variable){
return pair [1];
}
}
alert(查询变量+变量+找不到);
}
console.log(页面搜索:+ likePage);
console.log(Since:+ sinceDate)
//查找DOJOAPP FACEBOOK页面的位置从CHOSEN DATE起
$(document).ready function(){
getPostLikes()。then(function(){
// console.log(postArray);
pageThroughLikes(postArray,test)
});
});
函数test(postArray){
var convertedPostCSV = convertArrayOfObjectsToCSV(postArray);
downloadCSV(convertedPostCSV);
}
函数getPostLikes(response){
返回新的Promise(函数(履行,拒绝){
$ .get(https://graph.facebook .com / v2.8 /+ likePage +?fields = access_token,posts.since(+ sinceDate +){likes {id}}& access_token =+ facebookKey,function(facebookData){
var likePageId = facebookData.id;
var testPostArray = [];
if('posts'in facebookData){
var nextPage = facebookData.posts.paging.next;
var check = 0;
postArray.push(facebookData.posts.data);
var currentDataLength =
var i = 0
if('paging'in facebookData.posts) {
console.log(new page available);
do {
$ .ajax({
async:false,
type:GET,
url:nextPage,
success:function(nextPageData){
console.log(New Post Page Accessed:+ nextPage)
i ++;
console.log(Paging Through Posts:+ i)
testPostArray.push(nextPageData.data);
if('paging'in nextPageData){
nextPage = nextPageData.paging.next;
console.log(next page assigned:+ nextPage);
}
currentDataLength = nextPageData.data.length;
console.log(currentDataLength);
}
});
console.log(DATA LENGTH:+ currentDataLength);
} while(currentDataLength> 0);
testPostArray.forEach(function(element){
// console.log(element)
postArray.push(element);
fulfill();
}) ;
}
} else {
console.log('错误:从此日期起没有Facebook帖子!)
reject();
}
// console.log(postArray)
});
})
};
console.log(下载...)
函数pageThroughLikes(facebookPostArray,回调){
console.log('paging through likes')
var testArray = []
var promiseList = [];
facebookPostArray.forEach(function(array){
array.forEach(function(innerObject){
if('likes'in innerObject&&'paging'in innerObject.likes& &o''in innerObject.likes.paging){
var nextPage = innerObject.likes.paging.next;
console.log('new likes page assigned:'+ nextPage);
var currentPostId = innerObject.id;
var noMorePages = false;
var i = 0;
do {
$ .ajax({
url:nextPage,
success:function(nextLikePageData){
createLikeObject(nextLikePageData,currentPostId,checkForPagesOfLikes,nextLikePageData,noMorePages)
如果nextLikePageData.paging中的nextLikePageData&&'next'中的'paging'
nextPage = nextLikePageData.paging.next;
}
}
})
i + = 1
console.log(i)
} while(noMorePages = false);
}
})
});
console.log('paged through likes')
callback();
}
函数createLikeObject(likeData,postId,callback,args,fail){
likeArrayFormat = [];
likeObject = {};
likeObject.likes = {};
likeObject.id = postId;
likeObject.likes.data = []
likeData.data.forEach(function(like){
likeObject.likes.data.push(like);
});
likeArrayFormat.push(likeObject);
postArray.push(likeArrayFormat);
console.log('push new like data to postArray')
callback(args,fail)
}
function pushToArray(item,array,callback){
array.push(item);
callback()
}
function checkForPagesOfLikes(data,noMorePages){
if('paging'in data&&'&'next'in data.paging ){
return true;
console.log('NEW PAGE FOUND')
}
else {
noMorePages = true;
console.log(没有更多的页面对当前对象)
}
}
//自动下载CSV文件
函数downloadCSV args){
var data,filename,link;
var csv = convertArrayOfObjectsToCSV(postArray);
if(csv == null)return;
filename ='export.csv';
if(!csv.match(/ ^ data:text\ / csv / i)){
csv ='data:text / csv; charset = utf-8,'+ csv;
}
data = encodeURI(csv);
link = document.createElement('a');
link.setAttribute('href',data);
link.setAttribute('download',filename);
link.click();
}
// CONVERT FACEBOOK POSTS对象到CSV格式
函数convertArrayOfObjectsToCSV(args,callback){
var result,ctr,keys,columnDelimiter ,lineDelimiter,data;
// console.log(args)
data = args ||空值;
if(data == null ||!data.length){
return null;
}
columnDelimiter = args.columnDelimiter || ,;
lineDelimiter = args.lineDelimiter || \\\
;
keys = Object.keys(data [0]);
result ='';
result + =user_id+ columnDelimiter +post_id+ columnDelimiter +page_id;
result + = lineDelimiter;
// console.log(args)
args.forEach(function(object){
// console.log(object)
// console.log(object.length )
if(object.length!= 0){
object.forEach(function(item){
if('likes'in item&&'data'in item.likes ){
var postId = item.id;
item.likes.data.forEach(function(likeId){
if('id'in likeId){
//控制台.log(likeId)
var likeArray = likeId;
// console.log(likeArray)
result + = likeArray.id + columnDelimiter + postId.split('_')。reverse )+ lineDelimiter;
} else {
result + = columnDelimiter + postId.split('_')。reverse()+ lineDelimiter;
};
});
});
}
})
console.log('转换为CSV')
返回结果;
callback();
}
大问题是,目前, convertArrayObObjectsToCSV
在 createLikeObject
函数完成之前正在运行。思想回调将会起作用,但似乎我没有得到正确的一些。
这可能是一种方式因为你正在使用get call,所以我修改为不使用promise,因为ajax返回承诺。
这是一个伪代码,你可能需要调整一下才能使其运行
function pageThroughLikes(facebookPostArray){
var testArray = []
var promsieList = []
facebookPostArray.forEach(function(array){
array。 forEach(function(innerObject){
if('likes'in innerObject){
if('paging'in innerObject.likes){
if('next'in innerObject.likes.paging ){
nextPage = innerObject.likes.paging.next;
currentPostId = innerObject.id;
currentDataLength = innerObject.likes.data.length;
i = 0;
do {
promsieList.push(
$ .ajax({url:nextPage
}))
i + = 1;
} while(currentDataLength!= 0& ;& i> 10)
}
}
}
})
});
console.log('paged through likes')
return promiseList();
}
processData = function(nextLikePageData){
likeData = {};
likeData.id = currentPostId;
likeData.likes = {};
likeData.likes.data = nextLikePageData.data
likeData.likes.paging = nextLikePageData.paging
console.log(likeData)
testArray.push(likeData);
facebookPostArray.push(testArray);
console.log('push to postArray')
return likeData;
}
$(document).ready(function(){
getPostLikes()。then(function(){
$ .when.apply(this, pageThroughLikes(postArray))。done(function(){
//调试来检查参数对象,你会注意到它的数组数组
var testArray = []
$ .each (argument,function(k,v){
var dt = processData(v [0]);
testArray.push(dt);
facebookPostArray.push(dt);
$)
console.log(testArray)
test(testArray); // OR
test(facebookPostArray);
});
});
});
函数test(postArray){
var convertedPostCSV = convertArrayOfObjectsToCSV(postArray);
downloadCSV(convertedPostCSV);
}
编辑:
- 参数是由jquery $ .when
函数返回的默认对象。 - 这是一个子数组的集合,每个子数组由数据组成,
状态&承诺对象。 - 我假设您需要将所有get
调用的数据合并到一个testArray中,这样就可以在
调用
Have looked around and can't quite find the answer. I am trying to run a promise which executes a fulfil statement once all facebook api pages have been iterated through and saved to an array.
I know the function's ugly but I'm having a go to see how far I can get with promises.
function pageThroughLikes(facebookPostArray) {
var testArray = []
return new Promise(function (fulfill, reject) {
facebookPostArray.forEach(function(array) {
array.forEach(function(innerObject) {
if ('likes' in innerObject) {
if ('paging' in innerObject.likes) {
if ('next' in innerObject.likes.paging) {
nextPage = innerObject.likes.paging.next;
currentPostId = innerObject.id;
currentDataLength = innerObject.likes.data.length;
i = 0;
do{
$.get(nextPage, function(nextLikePageData) {
likeData = {};
likeData.id = currentPostId;
likeData.likes = {};
likeData.likes.data = nextLikePageData.data
likeData.likes.paging = nextLikePageData.paging
console.log(likeData)
testArray.push(likeData);
facebookPostArray.push(testArray);
console.log('pushed to postArray')
})
i += 1;
} while (currentDataLength != 0 && i > 10)
}
}
}
})
});
fulfill();
console.log('paged through likes')
})
}
At the moment once this function has completed I would like to run a 'test' function which converts my resulting array into CSV format and downloads a CSV file.
Here's my test function:
function test() {
var convertedPostCSV = convertArrayOfObjectsToCSV(postArray);
downloadCSV(convertedPostCSV);
}
And here's my running order for functions:
$(document).ready(function() {
getPostLikes().then(function() {
pageThroughLikes(postArray).then(function() {
test();
});
});
});
What I'm struggling with is that my 'test()' function is running before the data from new pages of likes is added to my 'facebookPostArray', or before the pageThroughLikes function ends.
Hoping someone can give me some advice/point me in the right direction.
EDIT
Ok, I have reformatted my code as @Winter Soldier has suggested but I'm still getting an uncaught error for the processData
function.
Here's my code:
function pageThroughLikes(facebookPostArray) {
console.log('paging through likes')
var testArray = []
var promiseList = [];
return new Promise(function (fulfill, reject) {
facebookPostArray.forEach(function(array) {
array.forEach(function(innerObject) {
if ('likes' in innerObject) {
if ('paging' in innerObject.likes) {
if ('next' in innerObject.likes.paging) {
nextPage = innerObject.likes.paging.next;
currentPostId = innerObject.id;
currentDataLength = innerObject.likes.data.length;
i = 0;
do{
promiseList.push(
$.ajax({url : nextPage
}))
i += 1;
} while (currentDataLength != 0 && i > 10)
}
}
}
});
return promiseList;
console.log('paged through likes')
})
processData = function(nextLikePageData){
likeData = {};
likeData.id = currentPostId;
likeData.likes = {};
likeData.likes.data = nextLikePageData.data
likeData.likes.paging = nextLikePageData.paging
console.log(likeData)
testArray.push(likeData);
facebookPostArray.push(testArray);
console.log('pushed to postArray')
return likeData;
}
$(document).ready(function() {
getPostLikes().then(function() {
$.when.apply(pageThroughLikes(postArray), this).done(function() {
var testArray = []
$.each(arguments, function(k, v){
var dt = processData(v[0]);
testArray.push(dt);
facebookPostArray.push(dt);
});
console.log(testArray)
test(testArray); // OR
test(facebookPostArray);
});
});
});
function test(postArray) {
var convertedPostCSV = convertArrayOfObjectsToCSV(postArray);
downloadCSV(convertedPostCSV);
}
EDIT 2.0
Here's my full code in all its ugliness. Just for reference. Figure the issue could be with something I have hidden away somewhere in the rest of my code so potentially worth showing you in full...
var facebookKey = config.FACEBOOK_KEY;
// ASSIGN QUERY TO VARIABLE
var likePage = getQueryVariable("likePage");
var sinceDate = getQueryVariable("sinceDate");
var likeArray = [];
var postArray = [];
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (pair[0] == variable) {
return pair[1];
}
}
alert("Query Variable " + variable + " not found");
}
console.log("Page Search: " + likePage);
console.log("Since: " + sinceDate)
// FIND DATA FOR FACEBOOK PAGE POSTS SINCE CHOSEN DATE
$(document).ready(function() {
getPostLikes().then(function() {
$.when.apply(pageThroughLikes(postArray), this).done(function() {
var testArray = []
$.each(arguments, function(k, v){
var dt = processData(v[0]);
testArray.push(dt);
facebookPostArray.push(dt);
});
console.log(testArray)
test(testArray); // OR
test(facebookPostArray);
});
});
});
function test(postArray) {
var convertedPostCSV = convertArrayOfObjectsToCSV(postArray);
downloadCSV(convertedPostCSV);
}
function getPostLikes(response) {
return new Promise(function (fulfill, reject) {
$.get("https://graph.facebook.com/v2.8/"+ likePage + "?fields=access_token,posts.since(" + sinceDate + "){likes{id}}&access_token=" + facebookKey, function (facebookData) {
var likePageId = facebookData.id;
var testPostArray = [];
if ('posts' in facebookData) {
var nextPage = facebookData.posts.paging.next;
var check = 0;
postArray.push(facebookData.posts.data);
var currentDataLength = " "
var i = 0
if ('paging' in facebookData.posts) {
console.log("new page available");
do {
$.ajax({
async: false,
type: "GET",
url: nextPage,
success: function(nextPageData) {
console.log("New Page Accessed: " + nextPage)
i++;
console.log("Page Number: " + i)
testPostArray.push(nextPageData.data);
if ('paging' in nextPageData) {
nextPage = nextPageData.paging.next;
console.log("next page assigned");
}
currentDataLength = nextPageData.data.length;
console.log(currentDataLength);
}
});
console.log("DATA LENGTH: " + currentDataLength);
} while (currentDataLength > 0);
testPostArray.forEach(function(element) {
console.log(element)
postArray.push(element);
fulfill();
});
}
} else {
console.log('Error: No facebook posts since this date!')
reject();
}
console.log(postArray)
});
})
};
console.log("Downloading...")
function pageThroughLikes(facebookPostArray) {
console.log('paging through likes')
var testArray = []
var promiseList = [];
facebookPostArray.forEach(function(array) {
array.forEach(function(innerObject) {
if ('likes' in innerObject) {
if ('paging' in innerObject.likes) {
if ('next' in innerObject.likes.paging) {
nextPage = innerObject.likes.paging.next;
currentPostId = innerObject.id;
currentDataLength = innerObject.likes.data.length;
i = 0;
do{
promiseList.push(
$.ajax({url : nextPage
}))
i += 1;
} while (currentDataLength != 0 && i > 10)
}
}
}
})
});
console.log('paged through likes')
return promiseList;
}
processData = function(nextLikePageData){
likeData = {};
likeData.id = currentPostId;
likeData.likes = {};
likeData.likes.data = nextLikePageData.data
likeData.likes.paging = nextLikePageData.paging
console.log(likeData)
testArray.push(likeData);
facebookPostArray.push(testArray);
console.log('pushed to postArray')
return likeData;
}
// AUTO DOWNLOAD CSV FILE
function downloadCSV(args) {
var data, filename, link;
var csv = convertArrayOfObjectsToCSV(postArray);
if (csv == null) return;
filename = args.filename || 'export.csv';
if (!csv.match(/^data:text\/csv/i)) {
csv = 'data:text/csv;charset=utf-8,' + csv;
}
data = encodeURI(csv);
link = document.createElement('a');
link.setAttribute('href', data);
link.setAttribute('download', filename);
link.click();
}
// CONVERT FACEBOOK POSTS OBJECTS TO CSV FORMAT
function convertArrayOfObjectsToCSV(args) {
var result, ctr, keys, columnDelimiter, lineDelimiter, data;
data = args || null;
if (data == null || !data.length) {
return null;
}
columnDelimiter = args.columnDelimiter || ',';
lineDelimiter = args.lineDelimiter || '\n';
keys = Object.keys(data[0]);
result = '';
result += "user_id" + columnDelimiter + " post_id" + columnDelimiter + " page_id";
result += lineDelimiter;
data.forEach(function(item) {
item.forEach(function(post) {
if ('likes' in post) {
var likeArray = post.likes
likeArray.data.forEach(function(like) {
result += like.id + columnDelimiter + post.id.split('_').reverse() + lineDelimiter;
});
} else {
result += columnDelimiter + post.id.split('_').reverse() + lineDelimiter;
};
});
});
console.log('converted to CSV')
return result;
}
EDIT 3.0
Function is almost fixed, only issue is that its not looping. Runs perfectly once though!
function pageThroughLikes(facebookPostArray) {
console.log('paging through likes')
var testArray = []
var promiseList = [];
// return new Promise(function (fulfill, reject) {
facebookPostArray.forEach(function(array) {
array.forEach(function(innerObject) {
if ('likes' in innerObject) {
if ('paging' in innerObject.likes) {
if ('next' in innerObject.likes.paging) {
nextPage = innerObject.likes.paging.next;
currentPostId = innerObject.id;
currentDataLength = innerObject.likes.data.length;
i = 0;
do{
promiseList.push(
$.ajax({url : nextPage
}).then(function(data, b, promise){
data.id = currentPostId;
if ('paging' in data) {
if ('next' in data.paging) {
nextPage = data.paging.next;
}
}
console.log(nextPage)
return promise;
}))
i += 1;
console.log(i)
} while (currentDataLength != 0 && i > 10)
}
}
}
})
});
console.log('paged through likes')
return promiseList;
}
Edit 4.0
Here's my current code...
Almost everything's finally working, managed to get a bunch of info from loads of late night googling.
var facebookKey = config.FACEBOOK_KEY;
// ASSIGN QUERY TO VARIABLE
var likePage = getQueryVariable("likePage");
var sinceDate = getQueryVariable("sinceDate");
var likeArray = [];
var postArray = [];
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (pair[0] == variable) {
return pair[1];
}
}
alert("Query Variable " + variable + " not found");
}
console.log("Page Search: " + likePage);
console.log("Since: " + sinceDate)
// FIND DATA FOR DOJOAPP FACEBOOK PAGE POSTS SINCE CHOSEN DATE
$(document).ready(function() {
getPostLikes().then(function() {
// console.log(postArray);
pageThroughLikes(postArray, test)
});
});
function test(postArray) {
var convertedPostCSV = convertArrayOfObjectsToCSV(postArray);
downloadCSV(convertedPostCSV);
}
function getPostLikes(response) {
return new Promise(function (fulfill, reject) {
$.get("https://graph.facebook.com/v2.8/"+ likePage + "?fields=access_token,posts.since(" + sinceDate + "){likes{id}}&access_token=" + facebookKey, function (facebookData) {
var likePageId = facebookData.id;
var testPostArray = [];
if ('posts' in facebookData) {
var nextPage = facebookData.posts.paging.next;
var check = 0;
postArray.push(facebookData.posts.data);
var currentDataLength = " "
var i = 0
if ('paging' in facebookData.posts) {
console.log("new page available");
do {
$.ajax({
async: false,
type: "GET",
url: nextPage,
success: function(nextPageData) {
console.log("New Post Page Accessed: " + nextPage)
i++;
console.log("Paging Through Posts: " + i)
testPostArray.push(nextPageData.data);
if ('paging' in nextPageData) {
nextPage = nextPageData.paging.next;
console.log("next page assigned: " + nextPage);
}
currentDataLength = nextPageData.data.length;
console.log(currentDataLength);
}
});
console.log("DATA LENGTH: " + currentDataLength);
} while (currentDataLength > 0);
testPostArray.forEach(function(element) {
// console.log(element)
postArray.push(element);
fulfill();
});
}
} else {
console.log('Error: No facebook posts since this date!')
reject();
}
// console.log(postArray)
});
})
};
console.log("Downloading...")
function pageThroughLikes(facebookPostArray, callback) {
console.log('paging through likes')
var testArray = []
var promiseList = [];
facebookPostArray.forEach(function(array) {
array.forEach(function(innerObject) {
if ('likes' in innerObject && 'paging' in innerObject.likes && 'next' in innerObject.likes.paging) {
var nextPage = innerObject.likes.paging.next;
console.log('new likes page assigned: ' + nextPage);
var currentPostId = innerObject.id;
var noMorePages = false;
var i = 0;
do{
$.ajax({
url: nextPage,
success: function(nextLikePageData) {
createLikeObject(nextLikePageData, currentPostId, checkForPagesOfLikes, nextLikePageData, noMorePages)
if ('paging' in nextLikePageData && 'next' in nextLikePageData.paging) {
nextPage = nextLikePageData.paging.next;
}
}
})
i += 1
console.log(i)
} while (noMorePages = false);
}
})
});
console.log('paged through likes')
callback();
}
function createLikeObject(likeData, postId, callback, args, fail) {
likeArrayFormat = [];
likeObject = {};
likeObject.likes = {};
likeObject.id = postId;
likeObject.likes.data = []
likeData.data.forEach(function(like) {
likeObject.likes.data.push(like);
});
likeArrayFormat.push(likeObject);
postArray.push(likeArrayFormat);
console.log('pushed new like data to postArray')
callback(args, fail)
}
function pushToArray(item, array, callback) {
array.push(item);
callback()
}
function checkForPagesOfLikes(data, noMorePages) {
if ('paging' in data && 'next' in data.paging) {
return true;
console.log('NEW PAGE FOUND')
}
else {
noMorePages = true;
console.log('NO MORE PAGES OF LIKES FOR CURRENT OBJECT')
}
}
// AUTO DOWNLOAD CSV FILE
function downloadCSV(args) {
var data, filename, link;
var csv = convertArrayOfObjectsToCSV(postArray);
if (csv == null) return;
filename = 'export.csv';
if (!csv.match(/^data:text\/csv/i)) {
csv = 'data:text/csv;charset=utf-8,' + csv;
}
data = encodeURI(csv);
link = document.createElement('a');
link.setAttribute('href', data);
link.setAttribute('download', filename);
link.click();
}
// CONVERT FACEBOOK POSTS OBJECTS TO CSV FORMAT
function convertArrayOfObjectsToCSV(args, callback) {
var result, ctr, keys, columnDelimiter, lineDelimiter, data;
// console.log(args)
data = args || null;
if (data == null || !data.length) {
return null;
}
columnDelimiter = args.columnDelimiter || ',';
lineDelimiter = args.lineDelimiter || '\n';
keys = Object.keys(data[0]);
result = '';
result += "user_id" + columnDelimiter + " post_id" + columnDelimiter + " page_id";
result += lineDelimiter;
// console.log(args)
args.forEach(function(object) {
// console.log(object)
// console.log(object.length)
if (object.length != 0) {
object.forEach(function(item) {
if ('likes' in item && 'data' in item.likes) {
var postId = item.id;
item.likes.data.forEach(function(likeId) {
if ('id' in likeId) {
// console.log(likeId)
var likeArray = likeId;
// console.log(likeArray)
result += likeArray.id + columnDelimiter + postId.split('_').reverse() + lineDelimiter;
} else {
result += columnDelimiter + postId.split('_').reverse() + lineDelimiter;
};
});
}
});
}
})
console.log('converted to CSV')
return result;
callback();
}
Big issue is that at the moment, convertArrayObObjectsToCSV
is running before the createLikeObject
function is complete. Thought callbacks would've worked but it seems I haven't got something quite right.
Here might be one way to do it since you are using get call, I've modified to not use promise at all because ajax returns the promise anyway.
It is a pseudo code, you might have to tweak it a bit to get it running
function pageThroughLikes(facebookPostArray) {
var testArray = []
var promsieList = []
facebookPostArray.forEach(function(array) {
array.forEach(function(innerObject) {
if ('likes' in innerObject) {
if ('paging' in innerObject.likes) {
if ('next' in innerObject.likes.paging) {
nextPage = innerObject.likes.paging.next;
currentPostId = innerObject.id;
currentDataLength = innerObject.likes.data.length;
i = 0;
do{
promsieList.push(
$.ajax({url : nextPage
}))
i += 1;
} while (currentDataLength != 0 && i > 10)
}
}
}
})
});
console.log('paged through likes')
return promiseList();
}
processData = function(nextLikePageData){
likeData = {};
likeData.id = currentPostId;
likeData.likes = {};
likeData.likes.data = nextLikePageData.data
likeData.likes.paging = nextLikePageData.paging
console.log(likeData)
testArray.push(likeData);
facebookPostArray.push(testArray);
console.log('pushed to postArray')
return likeData;
}
$(document).ready(function() {
getPostLikes().then(function() {
$.when.apply(this, pageThroughLikes(postArray)).done(function() {
//debug to examine the arguments object, you'll notice its an array of arrays
var testArray = []
$.each(arguments, function(k, v){
var dt = processData(v[0]);
testArray.push(dt);
facebookPostArray.push(dt);
});
console.log(testArray)
test(testArray); // OR
test(facebookPostArray);
});
});
});
function test(postArray) {
var convertedPostCSV = convertArrayOfObjectsToCSV(postArray);
downloadCSV(convertedPostCSV);
}
EDIT:
- The arguments is a default object returned by the jquery $.whenfunction.
- This is a collection of subarrays and each subarray consists of data,status & promise object.
- I'm assuming that you need to consolidate the data from all the getcalls into a testArray so, that is what is being done in the whencall
这篇关于为什么我的承诺不能同步运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!