【发布时间】:2021-03-11 19:31:54
【问题描述】:
我正在使用 TypeScript api 构建 Node JS,Express,我想在其中获取特定用户存储的数据。我想根据用户ID从mongodb获取reocrd。
我的 DataRouter.ts
router.get('/:userId', Data.findByUserId);
我的服务.ts
async findByUserId(id: string): Promise<IRecord[]> {
try {
return await DataModel.find({userId: id});
} catch (error) {
throw new Error(error.message);
}
},
我的数据组件
export async function findByUserId(req: Request, res: Response, next: NextFunction): Promise<void> {
try {
const record: IData[] = await DataService.findByUserId(req.params.id);
res.status(200).json(record);
} catch (error) {
next(new HttpError(error.message.status, error.message));
}
}
还有我的 IRepository.ts
findByUserId(id: string): Promise<T[]>;
我收到了错误
HttpError:转换为 ObjectId 的值失败 模型“DataModel”的路径“_id”处的“auth0|5fa05bf7657a9f006f9be77d”
如果有人有经验,我将非常感激,并分享您的知识。
【问题讨论】:
标签: node.js mongodb typescript express oauth