.findOne()
Attempt to find a particular record in your database that matches the given criteria.
var record = await Something.findOne(criteria);
Argument | Type | Details | |
---|---|---|---|
1 | criteria | 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 . |
Type | Description |
---|---|
The record that was found, or undefined if no such record could be located. |
Name | Type | When? |
---|---|---|
UsageError | Thrown if something invalid was passed in. | |
AdapterError | Thrown if something went wrong in the database adapter. | |
Error | Thrown if anything else unexpected happens. |
See Concepts > Models and ORM > Errors for examples of negotiating errors in Sails and Waterline.
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);
}
- 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 beundefined
.