【问题标题】:next js deploy vercel returns empty object when using cheerionext js deploy vercel 在使用 cheerio 时返回空对象
【发布时间】:2023-02-15 16:40:43
【问题描述】:

我想使用下一个 js 从 vercel 中的 API 服务器获取数据,当使用 localhost:3000 时,API 服务器可以工作,但是当部署时,API 服务器在域https://komik-two.vercel.app 中不工作

import axios from "axios";
import * as cheerio from "cheerio";
import { AxiosAPP, AxiosService, client } from "components/function/axios";
import { fetcher, fetcherAPI } from "components/function/fetch";
import NextCors from "nextjs-cors";




export default async function handler(req, res) {
  const link_endpoint = "https://komikcast.me/komik/";


  if (req.method === "GET") {
    try {
      await NextCors(req, res, {
        // Options
        methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"],
        origin: "*",
        optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
      });
      const  data  = await fetch("https://komikcast.me").then(res=>res.text());
      // console.log(data);

      const $ = cheerio.load(data);
      const element = $("#content");
      let komik_list = [];

      // console.log(element);
      let title, type, endpoint, last_upload_endpoint, thumb, chapter, rating;

      element.find(".swiper-wrapper > .swiper-slide").each((i, el) => {
        title = $(el)
          .find("a > .splide__slide-info")
          .find(".title")
          .text()
          .trim();
        type = $(el)
          .find("a > .splide__slide-image")
          .find(".type")
          .text()
          .trim();
        thumb = $(el).find("a > .splide__slide-image").find("img").attr("src");
        // thumb = $(el).find()
        endpoint = $(el).find("a").attr("href").replace(link_endpoint, "");
        chapter = $(el)
          .find("a > .splide__slide-info")
          .find(".other")
          .find(".chapter")
          .text()
          .trim();
        last_upload_endpoint = $(el)
          .find("a > .splide__slide-info")
          .find(".other")
          .find(".chapter")
          .attr("href");

        rating = $(el)
          .find("a > .splide__slide-info")
          .find(".other > .rate > .rating")
          .find(".numscore")
          .text();

        komik_list.push({
          endpoint,
          title,
          type,
          thumb,
          chapter,
          last_upload_endpoint,
          rating,
        });
      });
      res.statusCode = 200;
      res.json({
        status: true,
        message: "success",
        komik_list,
      });
    } catch (error) {
      res.status(404).json({ error });
    }
  }
}

http://localhost:3000/api/recommend在本地主机上成功显示对象 apihttps://komik-two.vercel.app/api/recommend在 vercel 中有效但返回 komik 列表对象:[]

{
status: true,
message: "success",
komik_list: [ ]
}

【问题讨论】:

    标签: reactjs api next.js cheerio vercel


    【解决方案1】:

    你解决了这个问题吗?如何解决的? 我有类似的问题,我也得到相同的返回值..

    在大量记录并试图找出问题所在之后,我注意到迭代元素的函数甚至没有运行。

    在你的情况下:

        element.find(".swiper-wrapper > .swiper-slide").each((i, el) => {...}
    

    就我而言

    list.each((i,e)=>{...}
    

    我什至试过这个:

    list.toArray().forEach((e)=>{...})
    

    我尝试从那些控制台.log,但没有记录任何东西..这个迭代器甚至不运行,它只是出于某种原因跳过它,甚至没有错误..什么都没有..就像它甚至不存在一样..

    【讨论】:

      猜你喜欢
      • 2022-08-12
      • 2011-09-02
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      相关资源
      最近更新 更多