NEXT.JS 如何重新验证/刷新数据

问题描述 投票:0回答:1

我正在使用 getServerSideProps() 获取数据,我不能使用 revalidate: 10 ,它只能用于 getStaticProps。那么,如何从 mongo 重新验证/刷新我的数据? 我在页面上有 btn,它改变了数据库中的值,但浏览器没有显示它。 如果我刷新页面,则会显示数据。 我正在尝试使用 SWR,但我做错了什么。 你能告诉我解决方案吗?谢谢。


export async function getServerSideProps() {

  await db.connect();
  const x =  await Team.find({}).lean() 
  await db.disconnect();

   return {
    props: {
      dataX: JSON.parse(JSON.stringify(x))
    } // , revalidate :10   <=== dont work for getServSideProps 
  }
}


const Index = ( props ) => {

const { dataX } = props  // data from mongo

let w = []
  const fetcher = async () => {
  let m = []
  await axios('/api/getTeam/data')
  .then(res => {
    m = res.data
  })
   return m
  }

function dataSWR() {
 const {data, error} = SWR('dashboard', fetcher)

 if(error) return 'error'
 if(!data) return 'loading'
 w.push(data)
}

dataSWR()

console.log(w)    // I am getting first empty [] them data
 
[]
[Array(8)]

```
mongodb next.js page-refresh swr
1个回答
0
投票

导出异步函数 getStaticProps() { const data = await fetchSomeData(); // Verileri getirme işlemi (örneğin, API çağrısı)

返回{ 道具: { 数据, }, revalidate: 10, // Verileri her 10 saniyede bir yeniden doğrula }; }

© www.soinside.com 2019 - 2024. All rights reserved.