Attempt to find a particular record in your database that matches the given criteria.
Something.findOne(criteria).exec(function (err, record) {
});
Argument | Type | Details | |
---|---|---|---|
1 | criteria | The first record which matches this Waterline criteria will be returned. |
Argument | Type | Details | |
---|---|---|---|
1 | err | The error that occurred, or null if there were no errors. |
|
2 | record | The record that was found, or undefined if no such record could be located. |
To locate the user whose username is "finn" in your database:
User.findOne({
username:'finn'
}).exec(function (err, finn){
if (err) {
return res.serverError(err);
}
if (!finn) {
return res.notFound('Could not find Finn, sorry.');
}
sails.log('Found "%s"', finn.fullName);
return res.json(finn);
});
- 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 the 2nd argument to the callback (e.g.finn
) will beundefined
.