我正在学习下一步,我正在制作一个简单的网站,使用表单输入用户,并将用户显示在相同的页面上。我使用api调用成功地完成了插入,但我不理解数据的获取,我在页面中创建了getserversideprops函数,在终端中我看到了数据,但是页面继续旋转,不加载,我该怎么办?谢谢大家
页码:
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
export default function Home({utenti}) {
return (
<>
<form method="POST" action='/api/sendInfo'>
<label>Nome</label>
<input type="text" name="nome"></input>
<label>Cognome</label>
<input type="text" name="cognome"></input>
<label>Email</label>
<input type="email" name="email"></input>
<button type="submit">Invia</button>
</form>
<div>
<p>Select Utenti</p>
<button type="submit" action = '/api/getInfo' method="GET"> prendi utenti</button>
</div>
</>
)
}
export async function getStaticProps () {
// Fetch data from external API
const res = await fetch('http:127.0.0.1:3000/api/getInfo')
const data = res.json()
console.log ("Questi sono i data");
console.log (data);
// res.json(data)
// Pass data to the page via props
return {
utenti: {
utenti: data
}
}
}api http:127.0.0.1:3000/api/getInfo代码:
import connection from "../../db";
export default async(req, res) => {
try{
const query = 'SELECT * FROM Utenti'
const result = await connection.query(
query,
);
console.log(result);
}catch(err){
console.log(err);
}
res.redirect("/");
}发布于 2022-09-30 14:55:46
res.json返回一个Promise,所以您需要对它进行await。
const data = await res.json();https://stackoverflow.com/questions/73910537
复制相似问题