Romeo Mihalcea
Romeo Mihalcea

Reputation: 10252

Async validation never returns in loopback

I have an async validation in one of my models in which I query for a related object to validate it's existence. The problem is that the request is timing out on this validation and the server never responds.

module.exports = function(Ip) {
      // Required fields
      Ip.validatesPresenceOf('server_id');

      ...

      Ip.validateAsync('server_id', isExistingServer, {
            message: 'invalid server'
      });

      function isExistingServer(err, done) {
            var ServerModel = Ip.app.models.Server;
            var self = this;
            process.nextTick(function() {
                  ServerModel.findById(self.server_id, function(e, server) {
                        console.log(_.isNull(server));// this actually prints false
                        return _.isNull(server) ? err() : done();
                  });
            });
      }
};

Upvotes: 1

Views: 466

Answers (2)

Carlos
Carlos

Reputation: 1277

According to documentation you should call done() after err. This is the example in docs:

User.validateAsync('name', customValidator, {message: 'Bad name'});

function customValidator(err, done) {
    process.nextTick(function () {
        if (this.name === 'bad') err();
        done();
    });
});

Note that as there is no return before the call to err(), done will also be called.

Upvotes: 2

Ignacio Orlandini
Ignacio Orlandini

Reputation: 31

Because you need execute done(); then err();

Upvotes: 3

Related Questions