Back to Sails

`.findOne()`

docs/reference/waterline/models/findOne.md

12.12.20002.4 KB
Original Source

.findOne()

Attempt to find a particular record in your database that matches the given criteria.

usage
var record = await Something.findOne(criteria);

Usage

ArgumentTypeDetails
1criteria((dictionary))The Waterline criteria to use for matching this record in the database. (This criteria must never match more than one record.) findOne queries do not support pagination using skip or limit.
Result
TypeDescription
((dictionary?))The record that was found, or undefined if no such record could be located.
Errors
NameTypeWhen?
UsageError((Error))Thrown if something invalid was passed in.
AdapterError((Error))Thrown if something went wrong in the database adapter.
Error((Error))Thrown if anything else unexpected happens.

See Concepts > Models and ORM > Errors for examples of negotiating errors in Sails and Waterline.

Example

To locate the user whose username is "finn" in your database:

javascript
var finn = await Users.findOne({
  username: 'finn'
});

if (!finn) {
  sails.log('Could not find Finn, sorry.');
}
else {
  sails.log('Found "%s"', finn.fullName);
}

Notes

  • This method can be used with await, promise chaining, or traditional Node callbacks.
  • Being unable to find a record with the given criteria does not constitute an error for findOne(). If no matching record is found, the result will be undefined.
<docmeta name="importance" value="10"> <docmeta name="displayName" value=".findOne()"> <docmeta name="pageType" value="method">