我正在try 通过Prisma查询我的MongoDB数据库.我想在我的查询中加入"财产"一词.Postman正在返回包含房产数据的正确数据,但在NextJs应用程序中,它似乎是未定义的或为空.我的疑问是:
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} />
</>
);
}
如果您需要任何其他信息,请让我知道.我已经被困在这个问题上好几个小时了
谢谢