findOne method

Future<Data> 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

Future<Data> findOne(
    [Map<String, dynamic> params,
    String errorMessage = 'No record was found matching the given query.']) {
  return index(params).then((result) {
    if (result == null) {
      throw AngelHttpException.notFound(message: errorMessage);
    } else {
      if (result.isEmpty) {
        throw AngelHttpException.notFound(message: errorMessage);
      } else {
        return result.first;
      }
    }
  });
}