Note that there are some explanatory texts on larger screens.

plurals
  1. PONodeJS and MongooseJS: accessing value from callback in validator
    primarykey
    data
    text
    <p>I'm trying to use validator and express-validator within a NodeJS/ExpressJS/MongoDB/Mongoose app to confirm a user isn't using an email address that's already been registered. I already have a unique index on the email field, but what I'm trying to do is keep all of my validation in one spot using one method. Thus, my question: validating uniqueness using express-validator.</p> <p>I've created the schema method to lookup the email address, and it's working. I've created the custom validator, and wired it up in the controller. It is also working. My problem is I don't know how to communicate to the validator in the controller the results of the schema method from the callback.</p> <p><strong>user.js (the model)</strong></p> <pre><code>... /** * Check for email addresses already in the collection */ checkEmailDupes: function(req, cb) { this.model('User').findOne({email: req}, function (err, user) { if (err) { return cb(err); } cb(null, user); // this is passing back the expected result }); }, ... </code></pre> <p><strong>users.js (the controller)</strong></p> <pre><code>... // The call to the custom validator (shown below) req.assert('email', 'Email must be unique').checkEmailDupes(user); ... // Check for email addresses that are already registered expressValidator.Validator.prototype.checkEmailDupes = function(user) { user.checkEmailDupes(this.str, function (err, result) { if (err) { console.log('An error occurred in checkEmailDupes'); } else { console.log('Found a user in checkEmailDupes'); console.log(result); // this is producing the expected result } }); return this.error(this.msg || 'Looks like this email address has already been registered'); return this; } </code></pre> <p>I know the <code>return this.error(this.msg...)</code> needs to go elsewhere. Ideally, I'd throw that into the callback, but when I do that I get </p> <blockquote> <p>TypeError: Object # has no method 'error'</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload