Back to Sails

`.catch()`

docs/reference/waterline/queries/catch.md

12.12.20002.3 KB
Original Source

.catch()

Execute a Waterline query instance using promises.

usage
.catch(callback)

As of Sails v1 and Node.js v8, you can take advantage of await instead of using this method.

Usage

ArgumentTypeDetails
1filter((dictionary?))An optional dictionary whose properties will be checked against the error. If they all match, then the callback will run. Otherwise, it won't.
2callback((function))A function that runs if the query fails.

Takes the error as its argument.

Callback
ArgumentTypeDetails
1err((Error?))The Error that occurred, or undefined if there were no errors.

Example

To look up the user with the specified email address:

javascript
User.findOne({
  email: req.param('email')
})
.then(function (user){
  if(!user) { return res.notFound(); }
  return res.json(user);
})
// If there was some kind of usage / validation error
.catch({ name: 'UsageError' }, function (err) {
  return res.badRequest(err);
})
// If something completely unexpected happened.
.catch(function (err) {
  return res.serverError(err);
});

Notes

  • Whenever possible, it is recommended that you use await instead of calling this method.
  • This is an alternative to .exec(). When combined with .then(), it provides the same functionality.
  • The .catch() function also returns a promise to allow for chaining. This is not recommended for any but the most advanced users of promises due to the complex (and arguably non-intuitive) behavior of chained .catch() calls.
  • For more information, see the bluebird .catch() api docs.
<docmeta name="displayName" value=".catch()"> <docmeta name="pageType" value="method">