javascriptreactjsnext.jsgoogle-trends

NEXTJS google trend API


Having trouble using the npm google trends api in NEXTJS

Not sure if this is the right way to go about it.

I made a new API route.

http://localhost:3000/api/trends

http://localhost:3000/api/trends

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 });
};

I get an error saying

TypeError: res.status is not a function

Solution

  • Try this. Here I am using TypeScript, if you are using Javascript, please edit code as needed.

    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)
        });
    }