Xi Xiao
Xi Xiao

Reputation: 973

gulp task hangs after job done in nodejs

I defined the simple gulp task that can be run in terminal with gulp load. The purpose is to get number of users that does not have location property set. However, after the number returned succesfully, the process hangs in terminal and I need to ctrl-c to stop it. Note, This is not a async call and uses mongoose plug-in to access DB.

gulp.task('load', function () {
    dbCall.getUserNumberWithoutLocation();
});

var getUserNumberWithoutLocation = function() {
    var query = User.find({ 'location': null });
    query.exec(function(err, users) {
        for (var i = 0; i < users.length; i++) {
            console.log(users[i].location);
        }
        console.log(users.length);
    })
};

Upvotes: 1

Views: 300

Answers (1)

Xi Xiao
Xi Xiao

Reputation: 973

Despite the prompt still does not return after gulp.task done (which probably is my local machine issue), I put my code here. The callback function is passed into the inner getUserNumberWithoutLocation function, in which the it is called once User.find is completed.

gulp.task('load', ['style'], function (callback) {
    dbCall.getUserNumberWithoutLocation(callback);
});
exports.getUserNumberWithoutLocation = function(callback) {
    var query = User.find({'location': null});
    query.exec(function(err, users) {
        console.log(users.length);
        if (err) {
            return callback(err);
        }
        callback();
    });
};

Upvotes: 1

Related Questions