簡體   English   中英

在后端使用快速 api 和 mongo; 為獲取請求獲取“value_error.missing”

[英]Using fast api and mongo in backend; getting “value_error.missing” for get request

我已經定義了我的獲取路線,如下所述

app = FastAPI()
@app.get('/audioFileType')
async def read_audioFileType(audioFileType: AudioFileType):
    audioList = []
    if hasattr(audioFileType, 'song') :
        for audiofile in db.audioList.find():
            audioList.append(Song(**audiofile))
        return { 'audioFileType': audioList }
         

我在后端使用 Mongo。 我的 model 結構如下。

class Song(BaseModel):
    id: PyObjectId = Field(alias='_id')
    name: str
    duration: int
    uploaded_time: datetime

class Podcast(BaseModel):
    id: PyObjectId = Field(alias='_id')
    name: str
    duration: int
    uploaded_time: datetime
    host: str
    participants: str

class AudioBook(BaseModel):
    id: PyObjectId = Field(alias='_id')
    title: str
    author: str
    narrator: str
    duration: int
    uploaded_time: datetime

class Config:
    arbitrary_types_allowed = True
    json_encoders = {
        ObjectId: str
    }

class AudioFileType(BaseModel):
    song: List[Song]
    podcast: List[Podcast]
    audioBook: List[AudioBook]

class audioFileMetadata(BaseModel):
    id: PyObjectId = Field(alias='_id')
    metadata = str

我收到如下錯誤:

"type": "value_error.missing"

任何人都可以對此提出他/她的寶貴意見嗎?

記錄並檢查 db.audioList.find() 的內容。 缺少一些必填字段。 比你可以使這個缺失的字段像這樣可選:

from typing import Optional


class Song(BaseModel):
    id: PyObjectId = Field(alias='_id')
    name: str
    duration: Optional[int] = None
    uploaded_time: datetime

暫無
暫無

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

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