Node.js-数组可以';在Netlify的活动中,每个人都不能被催促

Node.js-数组可以';在Netlify的活动中,每个人都不能被催促,node.js,aws-lambda,cheerio,netlify,netlify-function,Node.js,Aws Lambda,Cheerio,Netlify,Netlify Function,我正在创建一个项目,用截取的维基百科JP的Netlify函数数据创建TimelineJS TimelineJS需要JSON和它确定的模式,我从Wikiepdia JP中为JSON数据刮取了多个元素 我需要将这些元素设置为数组,并在HTTP请求时以文本形式返回正文 我写的代码在这里 const axios = require('axios') const cheerio = require('cheerio') const moment = require('moment') const ogs

我正在创建一个项目,用截取的维基百科JP的Netlify函数数据创建TimelineJS

TimelineJS需要JSON和它确定的模式,我从Wikiepdia JP中为JSON数据刮取了多个元素

我需要将这些元素设置为数组,并在HTTP请求时以文本形式返回正文

我写的代码在这里

const axios = require('axios')
const cheerio = require('cheerio')
const moment = require('moment')
const ogs = require('open-graph-scraper')

const url = 'https://ja.wikipedia.org/wiki/'

moment.locale('ja')

const getOGPImagePath = async (options) => {
  return await ogs(options)
    .then((response) => {
      // console.log(response)
      const ogImagePath = response.data.ogImage.url
      return ogImagePath
    })
    .catch((error) => {
      console.log(error)
    })
}

exports.handler = async (event, context) => {
  const date = event.queryStringParameters.date
  const endpoint = url + date
  const momentDate = moment(date, 'M月D日')
  const day = momentDate.format('D')
  const month = momentDate.format('M')
  const html = await axios.get(encodeURI(endpoint),{ headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100'}})
  const $ = await cheerio.load(html.data, { decodeEntities: false })
  let data = []

  $('.mw-parser-output > ul').first().find('li').not('.mw-empty-elt').each(async (i,elem) => {
    const year = String($(elem).text().match(/\d{1,4}年/)).replace(/年/, '')
    const content = String($(elem).text().match(/\-\s(.*)/))

    const contentHtml = $(elem).html().replace(/((^<a(.*)>\d{1,4}年<\/a>(.*)\s\-\s)|(^\d{1,4}年(.*)\s\-\s))/,'')

    const $c = await cheerio.load(contentHtml, { decodeEntities: false })

    const mainUrl = await 'https://ja.wikipedia.org' + $c('a').first().attr('href')

    const options = {
      url: mainUrl
    }

    // console.log(mainUrl) <- works fine

    const ogImagePath = await getOGPImagePath(options)

    // console.log(ogImagePath) <- works fine

    data.push({
      media: {  
        url: ogImagePath,
      },
      start_date: { 
        year: year,
        month: month,
        day: day
      },
      text: {
        text: contentHtml
      },  
      background: ogImagePath
    })

    // console.log(data) <- works fine
  })

  console.log(data) // <- Does not works fine. The result is '[]' empty array.

  return {
    statusCode: 200,
    body: JSON.stringify({ events: data }),
    headers: {
      'Content-Type': 'application/json; charset=utf8',
      'Access-Control-Allow-Origin': "*"
    }
  };
}
const axios=require('axios'))
const cheerio=require('cheerio')
常数力矩=要求(‘力矩’)
const ogs=require('open-graph-scraper')
常量url=https://ja.wikipedia.org/wiki/'
moment.locale('ja')
const getOGPImagePath=async(选项)=>{
返回等待ogs(选项)
。然后((响应)=>{
//console.log(响应)
const ogImagePath=response.data.ogImage.url
返回图像路径
})
.catch((错误)=>{
console.log(错误)
})
}
exports.handler=异步(事件、上下文)=>{
const date=event.queryStringParameters.date
const endpoint=url+日期
恒力矩日期=力矩(日期,'M月D日')
const day=momentDate.format('D')
const month=momentDate.format('M')
const html=await axios.get(encodeURI(endpoint),{头文件:{‘用户代理’:‘Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/69.0.3497.100’})
const$=await cheerio.load(html.data,{decodeEntities:false})
让数据=[]
$('.mw parser output>ul').first().find('li').not('.mw empty elt').each(异步(i,elem)=>{
const year=String($(elem).text().match(/\d{1,4}年/)).替换(/年/, '')
常量内容=字符串($(elem).text().match(/\-\s(.*)/)
const contentHtml=$(elem.html().replace(/(^\d{1,4}年(.*)\s\-\s)|(^\d{1,4}年(.\-\s))/,“”)
const$c=await cheerio.load(contentHtml,{decodeEntities:false})
const mainUrl=等待'https://ja.wikipedia.org'+$c('a').first().attr('href'))
常量选项={
url:mainUrl
}
//console.log(mainUrl)