Edit Page

.findOne()

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

var record = await Something.findOne(criteria);

Usage

#
Argument Type Details
1 criteria 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
#
Type Description
Dictionary? The record that was found, or undefined if no such record could be located.
Errors
#
Name Type When?
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:

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.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

Reference