我正在尝试使用pushshift api和axios JavaScript库提取Reddit数据。我不断收到错误消息:
UnhandledPromiseRejectionWarning:未处理的承诺拒绝。引发此错误的原因可能是抛出了一个没有catch块的异步函数,或者是拒绝了一个.catch()无法处理的承诺。 (拒绝ID:6)
下面是代码(FYI利用另一个人的现有代码,只是试图了解如何使用Reddit数据)。我认为通过在底部包括.catch函数,一切都会正常进行。感谢任何建议。
const axios = require("axios");
const axiosRetry = require("axios-retry");
const moment = require("moment");
const _ = require("lodash");
const Comment = require("Comment.js");
let startDate = new Date("Jan 1, 2015 00:00:00").getTime();
const endDate = new Date("July 27, 2018").getTime();
const limit = 50;
axiosRetry(axios, { retries: 3 });
// pushshift does not use milliseconds and date does, which is accounted for in substr & 1000
const fetchComments = async (startDate, limit) => {
console.log('initiating comment fetch from ${startDate} to ${endDate}');
while (startDate < endDate) {
//get the first limit comments after startDate
const response = await axios.get('https://apiv2.pushshift.io/reddit/search/comment/?q=elon%20musk&size=${limit}&after=${startDate.toString().substr(0, 10)}&before=${endDate.toString().substr(0, 10)}');
const comments = response.data.data;
// prep comments for db
const commentDocuments = comments.map(comment => {
return {
commentDate: moment.unix(comment.created_utc).format("MM/DD/YYYY"),
subreddit: comment.subreddit,
body: comment.body,
score: comment.score,
};
});
console.log('insert (${new Date(_.first(comments).created_utc * 1000)} -> ${new Date(_.last(comments).created_utc * 1000)})');
await Comment.insertMany(commentDocuments).catch(e => console.log(e));
startDate = _.last(comments).created_utc * 1000;
}
};
fetchComments(startDate, limit);