尝试使用 Axios 应用 GET 方法时出现错误 422。显然,输入的 JSON 内容是错误的信息。默认情况下,我需要让所有参数为 null 或具有某个值,以便能够使用任何参数组合对数据库进行查询。测试 API 时,一切正常。
方法:
import axios from 'axios'
export default {
name: 'UserConsulta',
data: function(){
return {
username: null,
movement: null,
movement_type: null,
movement_category: null,
dateFrom: null,
dateUntil: null,
amountFrom: null,
amountUntil: null
}
},
methods:{
getMovement: function(){
var self = this
let consulta_in = {
username: this.username,
movement: this.movement,
movement_type: this.movement_type,
movement_category: this.movement_category,
dateFrom: this.dateFrom,
dateUntil: this.dateUntil,
amountFrom: this.amountFrom,
amountUntil: this.amountUntil
}
axios.get("http://127.0.0.1:8000/user/consulta/")
.then((result) => {
self.query = result.data.query
})
.catch((error) => {
if (error.response.status == "400")
alert("ERROR 400: La búsqueda no arrojó resutlados");
})
},
created: function(){
this.username = this.$route.params.username
}
}
}
使用 FastAPI 的 api.get:
@api.get("/user/consulta/")
async def get_consulta(consulta_in: ConsultaIn):
consulta_in_db = get_movement(consulta_in)
if consulta_in_db == None:
raise HTTPException(status_code=404, detail="El usuario no existe")
elif consulta_in_db == []:
raise HTTPException(status_code=404, detail="Su búsqueda no arrojó resultados")
return consulta_in_db