findOne method

  1. @override
Future<Map<String, dynamic>> findOne([
  1. Map<String, dynamic>? params,
  2. String errorMessage = 'No record was found matching the given query.'
])

Retrieves the first object from the result of calling index with the given params.

If the result of index is null, OR an empty Iterable, a 404 AngelHttpException will be thrown.

If the result is both non-null and NOT an Iterable, it will be returned as-is.

If the result is a non-empty Iterable, findOne will return it.first, where it is the aforementioned Iterable.

A custom errorMessage may be provided.

Implementation

@override
Future<Map<String, dynamic>> findOne(
    [Map<String, dynamic>? params,
    String errorMessage =
        'No record was found matching the given query.']) async {
  var found = await collection.findOne(_makeQuery(params));

  if (found == null) {
    throw AngelHttpException.notFound(message: errorMessage);
  }

  return _jsonify(found, params);
}