如何将由Next.js API创建的JSON数据发送到新路由?

huangapple go评论118阅读模式
英文:

How to send json data created by nextjs api to a new route?

问题

I am a complete web development noob and I am also a bit new to Javascript.
I am trying to make a quizlet style website where users can input the subjects and specific topic they're not very good at and then they get practice questions back rendered in a flashcard.js file I have.

For this I wrote an api in nextjs that generates these practise questions, I know the api works because I see it print out these questions, however I can't figure out how to parse the data into the flashcard.js file.

This is what my handler in my api looks like:

  1. import { Configuration, OpenAIApi } from 'openai';
  2. import { NextApiRequest, NextApiResponse } from 'next';
  3. import { NextResponse } from 'next/server';
  4. import { useRouter } from 'next/router';
  5. export default async function handler(req, res) {
  6. const { subject, topic, exam_board, qualification } = req.body;
  7. const questions = await generateQuestions(subject, exam_board, qualification, topic);
  8. console.log(questions);
  9. console.log()
  10. res.status(200).json({ questions });
  11. }

I would like to try using useRouter but I can't use it within the handler so sort of open to any ideas.

I have been stuck on this for months so any help would be greatly appreciated.

英文:

I am a complete web development noob and I am also a bit new to Javascript.
I am trying to make a quizlet style website where users can input the subjects and specific topic they're not very good at and then they get practice questions back rendered in a flashcard.js file I have.

For this I wrote an api in nextjs that generates these practise questions, I know the api works because I see it print out these questions, however I can't figure out how to parse the data into the flashcard.js file.

This is what my handler in my api looks like:

  1. import { Configuration, OpenAIApi } from 'openai';
  2. import { NextApiRequest, NextApiResponse } from 'next';
  3. import { NextResponse } from 'next/server';
  4. import { useRouter } from 'next/router';
  5. export default async function handler(req, res) {
  6. const { subject, topic, exam_board, qualification } = req.body;
  7. const questions = await generateQuestions(subject, exam_board, qualification, topic);
  8. console.log(questions);
  9. console.log()
  10. res.status(200).json({ questions });
  11. }
  12. }

I would like to try using useRouter but I can't use it within the handler so sort of open to any ideas.

I have been stuck on this for months so any help would be greatly appreciated.

答案1

得分: 1

我认为你可以将结果存储在 state 中。

你需要从 React 中导入 useState

  1. import { useState } from 'React';

然后像这样声明你的状态。

  1. const [questions, setQuestions] = useState(<default-state-value>);

然后将其作为 prop 传递给 flashcard.js 组件。
你需要导入这个组件。

  1. import FlashCard from '&lt;path-to-flashcard.js&gt;';

然后调用该组件并将 questions 状态作为 prop 传递

  1. &lt;FlashCard questionsProp={questions} /&gt;

在该组件内部,你可以使用条件渲染,这样当你的状态加载了数据时,它将在 UI 中更新。

英文:

I think what you can do is store the results in a state.

You will need to import useState from React.

  1. import { useState } from &#39;React&#39;;

And declare your state like this.

  1. const [questions, setQuestions] = useState(&lt;default-state-value&gt;);

Then pass it as a prop to the flashcard.js component.
You will need to import the component.

  1. import FlashCard from &#39;&lt;path-to-flashcard.js&gt;&#39;

Then call that component and pass questions state as a prop

  1. &lt;FlashCard questionsProp={questions} /&gt;

And inside that component you can use conditional rendering so that when your state is loaded with the data, then it will get updated in the UI.

huangapple
  • 本文由 发表于 2023年5月24日 20:36:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定