gpt4 book ai didi

Javascript - for 循环中的 Axios。使用当前获取的响应进行下一次获取数据的迭代

转载 作者:行者123 更新时间:2023-12-01 00:58:17 25 4
gpt4 key购买 nike

我在第一次迭代中使用变量 previous_epoch 来获取今天之前的 reddit 帖子。下一次迭代 previous_epoch 必须包含响应数据中的日期。奇怪的是,前两次循环后日期没有改变

const getposts = async(user) => {
var dataArray = [];
var utc = new Date()
.toJSON()
.slice(0, 10)
.replace(/-/g, "/");

var previous_epoch;


for(let i=0; i <=5; i++) {
if(i == 0) {
console.log('i is 0');
previous_epoch = new Date(utc).getTime() / 1000;
console.log('p',previous_epoch); <--keeps doesn't work after second loop
}
else {
// console.log('else',dataArray);
previous_epoch = dataArray[dataArray.length-1].created_utc
console.log('else',previous_epoch);
}
await getdat(user,previous_epoch).then((resp) => {
let jsondat = resp.data.data;
dataArray = jsondat.concat(dataArray);
})
}
// console.log( dataArray);
var result = _.map( dataArray, function( o ) {
return o.id;
});
/* console.log("%j",result); */
}

函数 getdat()

const getdat = (user,epoch) => {

const sub_url = `https://api.pushshift.io/reddit/search/submission/?subreddit=${user}&limit=300&sort=desc&before=${epoch}`;
console.log('elZK',sub_url);
return new Promise((resolve, reject) => {
axios.get(sub_url)
.then(response => {
return resolve(response)
})
.catch(error => {
return reject(error.message)
})
})
}

这是fiddle

编辑 2: 我更改了逻辑,以便从 getdat 函数本身获取下一个日期

const getdat = (user,epoch) => {
let res= {};
const sub_url = `https://api.pushshift.io/reddit/search/submission/?subreddit=${user}&limit=300&sort=desc&before=${epoch}`;
console.log('elZK',sub_url);
return new Promise((resolve, reject) => {
axios.get(sub_url)
.then(response => {
let d = response.data.data;
let e = d[d.length - 1].created_utc;
console.log('e',e);
res.data = d;
res.epoch = e;
return resolve(res)
})
.catch(error => {
return reject(error.message)
})
})
}



export const getUserPosts = async(user) => {
var dataArray = [];
var utc = new Date()
.toJSON()
.slice(0, 10)
.replace(/-/g, "/");

var previous_epoch;
var epoch;

for(let i=0; i <=5; i++) {
if(dataArray.length === 0) {
console.log('i is 0');
previous_epoch = new Date(utc).getTime() / 1000;
console.log('p',previous_epoch);
}
else {
// console.log('else',dataArray);
previous_epoch = epoch;
console.log('else',previous_epoch);
}
const resp = await getdat(user,previous_epoch);
const jsondat = resp.data;
dataArray = jsondat.concat(dataArray);
epoch = resp.epoch;
}
// console.log( dataArray);
var result = _.map( dataArray, function( o ) {
return o.id;
});
console.log("%j",result);
}

最佳答案

在第一次迭代中,您会获取今天之前的帖子,因此结果中的最后一个帖子将具有在今天之前或等于今天的created_utc。下一次迭代只需重复您的第一个请求。

试试这个代码

for(let i=0; i <=5; i++) {
if(dataArray.length === 0) {
console.log('i is 0');
previous_epoch = new Date(utc).getTime() / 1000;
console.log('p',previous_epoch);
}
else {
// console.log('else',dataArray);
previous_epoch = dataArray[0].created_utc
console.log('else',previous_epoch);
}
const resp = await getdat(user,previous_epoch);
const jsondat = resp.data.data;
dataArray = jsondat.concat(dataArray);
}

关于Javascript - for 循环中的 Axios。使用当前获取的响应进行下一次获取数据的迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56342893/

25 4 0
文章推荐: python - 为什么我的 Flask 错误处理程序没有被调用?
文章推荐: javascript - 如何在js中创建文件对象?
文章推荐: gson - Robospice 不可能在没有服务的情况下启动 SpiceManager
文章推荐: jQuery slider 未捕获类型错误 : Object # has no method 'data'