英文:
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:
import { Configuration, OpenAIApi } from 'openai';
import { NextApiRequest, NextApiResponse } from 'next';
import { NextResponse } from 'next/server';
import { useRouter } from 'next/router';
export default async function handler(req, res) {
const { subject, topic, exam_board, qualification } = req.body;
const questions = await generateQuestions(subject, exam_board, qualification, topic);
console.log(questions);
console.log()
res.status(200).json({ questions });
}
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:
import { Configuration, OpenAIApi } from 'openai';
import { NextApiRequest, NextApiResponse } from 'next';
import { NextResponse } from 'next/server';
import { useRouter } from 'next/router';
export default async function handler(req, res) {
const { subject, topic, exam_board, qualification } = req.body;
const questions = await generateQuestions(subject, exam_board, qualification, topic);
console.log(questions);
console.log()
res.status(200).json({ questions });
}
}
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
。
import { useState } from 'React';
然后像这样声明你的状态。
const [questions, setQuestions] = useState(<default-state-value>);
然后将其作为 prop 传递给 flashcard.js 组件。
你需要导入这个组件。
import FlashCard from '<path-to-flashcard.js>';
然后调用该组件并将 questions
状态作为 prop 传递
<FlashCard questionsProp={questions} />
在该组件内部,你可以使用条件渲染,这样当你的状态加载了数据时,它将在 UI 中更新。
英文:
I think what you can do is store the results in a state
.
You will need to import useState
from React.
import { useState } from 'React';
And declare your state like this.
const [questions, setQuestions] = useState(<default-state-value>);
Then pass it as a prop to the flashcard.js component.
You will need to import the component.
import FlashCard from '<path-to-flashcard.js>'
Then call that component and pass questions
state as a prop
<FlashCard questionsProp={questions} />
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论