Edit Page

.catch()

Execute a Waterline query instance using promises.

.catch(callback)

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

Usage

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

Takes the error as its argument.
Callback
Argument Type Details
1 err The Error that occurred, or undefined if there were no errors.

Example

To look up the user with the specified email address:

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.

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 repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Check out the official Sails VS Code extension

Documentation

Reference

Sails logo

Reference

AltStyle によって変換されたページ (->オリジナル) /