首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nextjs getServerSideProps即使在服务器重新启动后也不更新

Nextjs getServerSideProps即使在服务器重新启动后也不更新
EN

Stack Overflow用户
提问于 2022-06-24 15:24:37
回答 1查看 58关注 0票数 -2

我正在构建一个电子商务站点,但是在我重新启动nextjs服务器(使用server或npm )之后,leggings页面没有加载。在使用getproducts之后,leggings页面开始加载。

这是leggings.js

代码语言:javascript
复制
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,然后只有leggings页面才能工作。我正在使用mongodb数据库,连接工作正常。如何解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2022-06-24 15:54:36

我找到了办法。我只是忘了把!放在前面:mongoose.connections[0].readyState

示例:

代码语言:javascript
复制
if (!mongoose.connections[0].readyState)

现在,它可以正常工作,但是在服务器重新启动之后,加载页面需要一些时间。如何解决这个问题

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72746251

复制
相关文章

相似问题

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