View our GitHub

Please visit sails-docs on GitHub to view documentation on your mobile device.

Edit Page

.findOne()

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

Something.findOne(criteria).exec(function (err, record) {

});

Usage

Argument Type Details
1 criteria The first record which matches this Waterline criteria will be returned.
Callback
Argument Type Details
1 err The error that occurred, or undefined if there were no errors.
2 record The record that was found, or undefined if no such record could be located.

Example

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

User.findOne({
  username:'finn'
}).exec(function (err, finn){
  if (err) {
    return res.negotiate(err);
  }
  if (!finn) {
    return res.notFound('Could not find Finn, sorry.');
  }

  sails.log('Found "%s"', finn.fullName);
  return res.json(finn);
});

Notes

  • Being unable to find a record with the given criteria does not constitute an error for findOne(). If no matching record is found, the value of record in the callback 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-docs repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Reference

We wrote a book!

Get early access to the book
with promotion code: mcneildoc

Get the Book