Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Latest commit

 

History

History
57 lines (35 loc) · 1.73 KB

findOne.md

File metadata and controls

57 lines (35 loc) · 1.73 KB

.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 ((dictionary)) The first record which matches this Waterline criteria will be returned.
Callback
Argument Type Details
1 err ((Error?)) The error that occurred, or undefined if there were no errors.
2 record ((dictionary?)) 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.