首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Problema getServerSideProps nextjs

Problema getServerSideProps nextjs
EN

Stack Overflow用户
提问于 2022-09-30 14:52:06
回答 1查看 49关注 0票数 0

我正在学习下一步,我正在制作一个简单的网站,使用表单输入用户,并将用户显示在相同的页面上。我使用api调用成功地完成了插入,但我不理解数据的获取,我在页面中创建了getserversideprops函数,在终端中我看到了数据,但是页面继续旋转,不加载,我该怎么办?谢谢大家

页码:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
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("/");  
}
EN

回答 1

Stack Overflow用户

发布于 2022-09-30 14:55:46

res.json返回一个Promise,所以您需要对它进行await

代码语言:javascript
复制
const data = await res.json();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73910537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档