mongoose#isValidObjectId TypeScript Examples
The following examples show how to use
mongoose#isValidObjectId.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: middlewares.ts From foodie with MIT License | 6 votes |
function validateObjectID(...ObjectIDs) {
return function (req: any, res: any, next: NextFunction) {
ObjectIDs.forEach((id) => {
if (!isValidObjectId(req.params[id])) {
return next(new ErrorHandler(400, `ObjectID ${id} supplied is not valid`));
} else {
next();
}
});
}
}
Example #2
Source File: application.service.ts From uniauth-backend with MIT License | 5 votes |
async findOneById(id: string) {
if (isValidObjectId(id)) {
const item = await this.applicationModel.findOne({ _id: id });
return item;
} else {
throw new BadRequestException();
}
}
Example #3
Source File: PostSchema.ts From foodie with MIT License | 5 votes |
PostSchema.methods.isPostLiked = function (this: IPost, userID) {
if (!isValidObjectId(userID)) return;
return this.likes.some(user => {
return user._id.toString() === userID.toString();
});
}
Example #4
Source File: UserSchema.ts From foodie with MIT License | 5 votes |
UserSchema.methods.isBookmarked = function (this: IUser, postID) {
console.log(postID)
if (!isValidObjectId(postID)) return 'hehe';
return this.bookmarks.some(bookmark => {
return bookmark._id.toString() === postID.toString();
});
}