簡體   English   中英

Nextjs getServerSideProps 即使在服務器重新啟動后也不會更新

[英]Nextjs getServerSideProps not updating even after server restart

我正在構建一個電子商務網站,但是在我重新啟動 nextjs 服務器(使用 yarn dev 或 npm run dev)后,leggings 頁面沒有加載。 使用 getproducts api 后,leggings 頁面開始加載。

這是leggings.js

import React from 'react'
import Link from 'next/link'
import Product from "/models/Product"
import mongoose from 'mongoose'

const Leggings = ({ products }) => {
    return (
        <div>
            <section className="text-gray-600 body-font min-h-screen">
                <div className="container px-5 py-24 mx-auto">
                    <div className="flex flex-wrap -m-4 justify-center">
                        {products.map((item) => {

                            return <Link passHref={true} key={item._id} href={`/product/${item.slug}`}><div className="lg:w-1/5 md:w-1/2 p-4 w-full cursor-pointer shadow-lg m-7">
                                <a className="block relative rounded overflow-hidden">
                                    <img alt="ecommerce" className="m-auto md:m-0 h-[30vh] md:h-[36vh] block" src={item.img} />
                                </a>
                                <div className="mt-4 text-center md:text-left">
                                    <h3 className="text-gray-500 text-xs tracking-widest title-font mb-1">{item.category.toUpperCase()}</h3>
                                    <h2 className="text-gray-900 title-font text-lg font-medium">{item.title}</h2>
                                    <p className="mt-1">₹{item.price}</p>
                                    <p className="mt-1">S, M, L, XL, XXL</p>
                                </div>
                            </div>
                            </Link>
                        })}
                    </div>
                </div>
            </section>
        </div>
    )
}

export async function getServerSideProps(context) {
    if (mongoose.connections[0].readyState) {
        await mongoose.connect(process.env.MONGO_URI)
    }
    let products = await Product.find({ category: 'leggings' })
    return {
        props: { products: JSON.parse(JSON.stringify(products)) }, // will be passed to the page component as props
    }
}

export default Leggings

每次重新啟動 nextjs 服務器后,我都必須使用 getproducts api,然后只有緊身褲頁面才能工作。 我正在使用 mongodb 數據庫並且連接工作正常。 如何解決這個問題?

我找到了一個方法。 我只是忘記放了! 之前: mongoose.connections[0].readyState

例子:

if (!mongoose.connections[0].readyState)

現在它工作正常,但在服務器重新啟動后,加載頁面需要一些時間。 如何解決這個問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM