【问题标题】:NEXTJS google trend APINEXTJS 谷歌趋势 API
【发布时间】:2021-09-17 19:41:48
【问题描述】:

在 NEXTJS 中使用 npm google 趋势 api 时遇到问题

不确定这是否是正确的方法。

我创建了一个新的 API 路由。

http://localhost:3000/api/trends

http://localhost:3000/api/趋势

import googleTrendsApi from "google-trends-api";

const handler = async (res, req) => {
  const data = await googleTrendsApi
    .interestOverTime({ keyword: ["Women's march", "Trump Inauguration"] })
    .then(function (results) {
      console.log("These results are awesome", results);
    });

  res.status(200).json({ data: data });
};

我收到一个错误提示

TypeError: res.status is not a function

【问题讨论】:

  • handler 的签名应该是(req, res)res 是第二个参数而不是第一个 - 顺序很重要。 req.status(来自第一个参数)不存在因此错误的原因。

标签: javascript reactjs next.js google-trends


【解决方案1】:

试试这个。这里我使用的是 TypeScript,如果你使用的是 Javascript,请根据需要编辑代码。

import type { NextApiRequest, NextApiResponse } from 'next'
import googleTrendsApi from "google-trends-api";

type Data = {
  name: string
}

export default function handler(
  req: NextApiRequest,
  res: NextApiResponse<Data>
) {
  const data = googleTrendsApi
    .interestOverTime({ keyword: ["Women's march", "Trump Inauguration"] })
    .then(function (results: any) {
      res.status(200).json(results)
    });
}

【讨论】:

  • 谢谢!只是出于兴趣,如果你是使用什么库来绘制图表?
  • 创建图表?那里有很多库,但我认为使用 react-chartjs 会很好。
猜你喜欢
  • 2021-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-04
  • 2013-01-08
  • 1970-01-01
相关资源
最近更新 更多