我正在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} />
    </>
  );
}

如果您需要任何其他信息,请让我知道.我已经被困在这个问题上好几个小时了

谢谢

推荐答案

对于任何一个将来回到这里的人来说...

正如Radmehr上面所说的,我从FETCH函数中删除了以下代码:

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

然而,我发现问题在于Next缓存的FETCH功能.为了解决这个问题,我添加了缓存:‘no-store’

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

这对我很管用

Mongodb相关问答推荐

带dropTarget的MongoDB renameCollection命令是原子的吗

我无法在react 中使用 fetch 和express 从数据库中删除数据

MongoDB PHP UTF-8 问题

为什么使用整数作为 pymongo 的键不起作用?

如何在 Mongo 聚合中合并文档中的数组字段

用 Redis 查询?

Mongoose 与 mongodb 如何返回刚刚保存的对象?

用 BsonRepresentation(BsonType.ObjectId) vs BsonId vs ObjectId 在 C# 中装饰属性之间的区别

使用 brew upgrade Mongo update from 3.4 to 4.0 报错:在try 升级到 4.0 之前,数据文件需要完全升级到 3.6 版

Clojure 和 NoSQL 数据库

有没有办法将python对象直接存储在mongoDB中而不序列化它们

Django admin 和 MongoDB,可能吗?

如何使用 mgo 和 Go 查询 MongoDB 的日期范围?

错误:Could not connect to any servers in your MongoDB Atlas cluster

使用 MongoDB 的 map/reduce 来分组两个字段

Flask:设置应用程序和请求特定的属性?

如何在mongoose的嵌套填充中 Select 特定字段

对于社交网站(使用 Ruby on Rails 开发)来说,MongoDB 会是一个好主意吗?

Meteor 发布/订阅独特客户端集合的策略

即使重新安装后,Mongo 仍在等待 27017