本文整理汇总了TypeScript中mongoose.Model.findOne方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Model.findOne方法的具体用法?TypeScript Model.findOne怎么用?TypeScript Model.findOne使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongoose.Model
的用法示例。
在下文中一共展示了Model.findOne方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: reject
return new Promise<T[]>((resolve: Function, reject: Function) => {
const txModelOptions = this.obtainTransactionModelOptions(newOptions);
const authorizationResponse = this.isSearchAuthorized(txModelOptions);
if (!authorizationResponse.isAuthorized) {
return reject(new Error(authorizationResponse.errorMessage));
}
this.addAuthorizationDataPreSearch(txModelOptions);
this.transactionModelOptionsAddData(data, txModelOptions);
const search = this.obtainSearchExpression(data, txModelOptions);
if (Object.keys(search).length < 1) {
return reject(new Error('At least one filter value should be specified'));
}
this.Model.findOne(search, txModelOptions.projection,
{ sort: newOptions.sort, lean: true }).populate(txModelOptions.population)
.exec((err: Error, foundObj: T) => {
if (err) {
return reject(err);
}
if (ObjectUtil.isBlank(foundObj)) {
return reject(new Error('Object not found'));
}
if (txModelOptions.validatePostSearchAuthData) {
const authorizationResponse = this.validateAuthDataPostSearch(txModelOptions, foundObj);
if (!authorizationResponse.isAuthorized) {
return reject(new Error(authorizationResponse.errorMessage));
}
}
resolve(foundObj);
});
});
示例2: list
export function list(req: Request, res: Response): void {
let handle: string = UTIL.getRequestParam(req, req.routeVar.userHandleKey)
if (handle && handle.length > 0) {
const UserModel: Model<IUser> = UTIL.getModelFromName(req.routeVar.creatorType)
UserModel
.findOne({handle})
.select('_id')
.lean()
.then((user: IUser) => {
if (user) {
search(req, res, user._id)
} else {
res.status(404).json({ code: ERRORS.LOGIN.USER_NOT_FOUND })
}
})
.catch((err: Error) => {
res.status(res.statusCode).send()
console.log(err)
})
} else {
search(req, res)
}
}
示例3: retract
export function retract(req: Request, res: Response): void {
const [creator, creatorRef] = UTIL.getLoginedUser(req),
target: Schema.Types.ObjectId = req.body.id,
targetRef: string = req.body.type,
DataModel: Model<IContent> = UTIL.getModelFromName(targetRef)
let log: any = {
creator,
creatorRef,
target,
targetRef,
action: CONST.USER_ACTIONS.CONSUMER.RETRACT,
ua: req.body.ua || req.ua
}
DataModel
.findOne({
creator,
_id: target
})
.then((data: IContent) => {
if (data) {
if (data.status === CONST.STATUSES.CONTENT.EDITING) {
res.status(422).json({ code: ERRORS.CONTENT.CONTENT_CANNOT_BE_RETRACTED })
return null
} else {
data.status = CONST.STATUSES.CONTENT.EDITING
return data.save()
}
}
res.status(404).send({ code: ERRORS.CONTENT.NO_ELIGIBLE_CONTENT_FOUND })
return null
})
.then((data: IContent) => {
res.status(200).json(data)
new Logger(log)
// retract submission/approval process if required
if (CONFIG.POST_REQUIRES_APPROVAL) {
return Process
.findOneAndUpdate({
creator: log.creator,
creatorRef: log.creatorRef,
target: log.target,
targetRef: log.targetRef,
type: CONST.PROCESS_TYPES.APPROVAL
}, {
status: CONST.STATUSES.PROCESS.CANCELLED,
completed: UTIL.getTimestamp()
})
.sort({_id: -1})
} else {
return null
}
})
.catch((err: Error) => {
new Err(res, err, log)
})
}
示例4: async
return async (ctx: Context): Promise<[Document, RatingDocument]> => {
const query = model.findOne({ id: sanitize(ctx.params.id) });
if (populate) {
query.populate(populate);
}
const entity = await query.exec();
if (!entity) {
throw new ApiError('No such %s with ID "%s"', modelName, ctx.params.id).status(404);
}
const q = {
_from: ctx.state.user._id,
['_ref.' + modelName]: entity._id,
};
const rating = await state.models.Rating.findOne(q);
return [entity, rating];
};
示例5: async
return async (ctx: Context): Promise<[MetricsDocument, StarDocument]> => {
const query = model.findOne({ id: ctx.params.id });
if (populate) {
query.populate(populate);
}
const entity = await query.exec();
if (!entity) {
throw new ApiError('No such %s with ID "%s"', modelName, ctx.params.id).status(404);
}
const q = {
['_ref.' + modelName]: entity._id,
_from: ctx.state.user._id,
type: modelName,
};
const star = await state.models.Star.findOne(q);
return [entity, star];
};
示例6: comments
export function comments(req: Request, res: Response): void {
let slug: string = req.params.slug,
opt: any = UTIL.assembleSearchParams(req),
match: any = {}
if (req.query.hasOwnProperty('keywords') && req.query.keywords.length > 0) {
let query: string[] = req.query.keywords.split(',')
match = {content: {$in: query}}
}
const DataModel: Model<IContent> = UTIL.getModelFromName(req.routeVar.contentType)
DataModel
.findOne({slug})
.select(CONST.COMMENT_PARENT_FIELD_LIST)
.populate('creator', CONST.PUBLIC_CONSUMER_INFO_LIST)
.populate({
path: 'comments',
model: 'Comment',
match,
options: {
sort: {'_id': -1},
limit: opt.limit,
skip: opt.skip
},
populate: {
path: 'creator',
model: 'Consumer',
select: CONST.PUBLIC_CONSUMER_INFO_LIST
}
})
.lean()
.then((data: IContent) => {
if (data) {
res.status(200).json(data)
} else {
res.status(404).send()
}
})
.catch((err: Error) => {
res.status(res.statusCode).send()
console.log(err)
})
}
示例7: sublist
export function sublist(req: Request, res: Response): void {
const slug: string = req.params.slug,
path: string = req.params.sublist,
opt: any = UTIL.assembleSearchParams(req)
if (CONST.SUBLISTS.indexOf(path) > -1) {
const DataModel: Model<IContent> = UTIL.getModelFromName(req.routeVar.contentType)
DataModel
.findOne({slug})
.select(CONST.LIKE_PARENT_FIELD_LIST)
.populate('creator', CONST.PUBLIC_CONSUMER_INFO_LIST)
.populate({
path,
model: UTIL.getModelNameFromPath(path),
group: 'type',
options: {
sort: {'_id': -1},
limit: opt.limit,
skip: opt.skip
},
populate: {
path: 'creator',
model: 'Consumer',
select: CONST.PUBLIC_CONSUMER_INFO_LIST
}
})
.lean()
.then((data: IContent) => {
if (data) {
res.status(200).json(data)
} else {
res.status(404).send()
}
})
.catch((err: Error) => {
res.status(res.statusCode).send()
console.log(err)
})
} else {
res.status(404).send()
}
}
示例8: isUnique
export function isUnique(req: Request, res: Response): void {
let [result, query] = validateQuery(req.body)
if (!result) {
res.status(400).send(query)
} else {
const UserModel: Model<IUser> = UTIL.getModelFromName(req.routeVar.userType)
UserModel
.findOne(query)
.then((data: IUser) => {
let isAvailable: boolean = !(data)
res.status(200).json({isAvailable})
})
.catch((err: Error) => {
res.status(res.statusCode).send()
console.log(err)
})
}
}