freeCodeCamp运动追踪器项目:GET接口参数测试未通过求助
运动追踪器项目测试不通过问题
我在完成freeCodeCamp的运动追踪器项目时,卡在了这个测试项:
你可以为GET /api/users/:_id/logs请求添加from、to和limit参数,以获取任意用户的部分日志。from和to为yyyy-mm-dd格式的日期,limit为指定返回日志数量的整数。
预期响应示例:
{ "username": "fcc_test", "count": 1, "_id": "5fb5853f734231456ccb3b05", "log": [{ "description": "test", "duration": 60, "date": "Mon Jan 01 1990" }] }
我返回的结果和示例格式一致,但测试始终不通过。以下是我的数据库查询函数:
export const fetchExerciseLogs = async ( userId: string, from?: string, to?: string, limit?: string ): Promise<FetchExerciseLogsResult | undefined> => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const foundId: User | null = await UserModel.findById(userId) // using username to find exercises associated with it const exerciseQuery: exerciseQuery = {} if (foundId) { exerciseQuery.username = foundId.username; } // if there are request queries for date, add those to the query object if (from && to) { const fromDate = new Date(from); const toDate = new Date(to); exerciseQuery.date = { $gte: fromDate, $lte: toDate }; } // if there is a limit query, change it to a number let limitNumber: number = 9999 if (limit) { limitNumber = parseFloat(limit); } // find all exercises in the db that match the username and any date and/or limit queries const foundExercises = await ExerciseModel.find(exerciseQuery).limit(limitNumber).exec() let logArray: ExerciseDetails[] | undefined = foundExercises.map((exercise) => { return { description: exercise.description, duration: exercise.duration, date: exercise.date, }; }) // if there's a limit query - return log array no longer than the limit if (limitNumber) { logArray = logArray.slice(0, limitNumber + 1) } const numOfExercises: number = logArray.length; const exerciseLog: FetchExerciseLogsResult = { username: foundExercises[0]?.username || "no username found", count: numOfExercises, _id: userId, log: logArray.length > 0 ? logArray : [] } return exerciseLog };
我已经尝试把返回的log字段中“no logs found”替换为空数组,但测试还是不通过,希望有人能帮忙排查问题。
内容的提问来源于stack exchange,提问作者nic-ole
相关产品推荐
相关产品推荐

