接下来是js,MongoDB和Prisma,包括不为相关字段工作

qojgxg4l  于 2023-10-16  发布在  Go
关注(0)|答案(2)|浏览(100)

我正在尝试通过Prisma查询我的MongoDB数据库。我想在我的查询中包含“财产”。Postman正在返回包含属性数据的正确数据,但在NextJs应用程序中,它似乎是未定义的或null。我的问题是:

export async function GET(request: NextRequest) {
    const maintenanceItems = await prisma.maintenance.findMany({
        include: {
            property: true,
        }
    })
    return NextResponse.json(maintenanceItems)
}

我是这么说的

let maintenanceItems: NewMaintenanceIncidentType[] = [];
export const getMaintenanceItems = async () => {
    try {
        const response = await fetch(`${process.env.BASE_URL}/api/maintenance/`, {
            method: "GET",
            headers: {
                "Content-Type": "application/json",
            },
        });
        if (!response.ok) {
            throw new Error("Failed to fetch Maintenance Items");
        }

        maintenanceItems = await response.json();
        console.log("from the server: ", maintenanceItems);

    } catch (error) {
        console.log("Server fetch failed on OpenMaintenanceItemPage ", error);
    }
    return maintenanceItems
};

我的模特是:

model Maintenance {
  id          String   @id @default(auto()) @map("_id") @db.ObjectId
  name        String
  description String 
  status      String
  image       String?
  createdDate DateTime @default(now())
  dueDate     DateTime
  property    Property @relation(fields: [propertyId], references: [id])
  propertyId  String   @db.ObjectId
}

model Property {
  id               String         @id @default(auto()) @map("_id") @db.ObjectId
  propertyType     String 
  streetNumber     String 
  street           String
  postcode         String
  apartmentNumber  String? 
  city             String
  county           String
  propertyCategory String 
  status           String
  maintenanceTasks Maintenance[]
  userProperties   UserProperty[]

}

我喜欢的类型

export type NewMaintenanceIncidentType = {
    id: string,
    name: string,
    description: string,
    status: string,
    image: string,
    createdDate: Date,
    dueDate: Date,
    property: PropertyType,
    reportedBy: string,
    assignedTo: string,
    propertyId: string,
}

export type PropertyType = {
    id: string;
    propertyType: string;
    streetNumber: string;
    street: string;
    postcode: string;
    apartmentNumber: string;
    city: string;
    county: string;
    propertyCategory: string;
    status: string;
}

这就是我如何通过服务器页面获取

export default async function OpenMaintenanceItemsPage() {
  const openMaintenanceItems = await getMaintenanceItems();

  return (
    <>
      <Typography>Open Maintenance Items Page</Typography>
      <BasicTable openMaintenanceItems={openMaintenanceItems} />
    </>
  );
}

如果你需要任何其他信息让我知道。我已经在这里呆了几个小时了:D:D
谢谢

wvyml7n5

wvyml7n51#

首先从${process.env.BASE_URL}/api/maintenance/获取一个日志,它可以在基url之后有两个//
第二,在GET方法中不需要内容类型,因为你不能在get方法中传递数据:)

cmssoen2

cmssoen22#

对于任何一个在未来回到这里的人...
就像Radmehr上面说的,我从我的fetch函数中删除了以下内容:

headers: {
                "Content-Type": "application/json",
            },

我发现这个问题与Next如何缓存获取函数有关。为了解决这个问题,我添加了缓存:'无商店'

const response = await fetch(`${process.env.BASE_URL}/api/maintenance/`, { cache: 'no-store' });

对我有用的

相关问题