sql >> Base de Datos >  >> NoSQL >> MongoDB

La devolución de llamada de Node.js con la actualización de MongoDB nunca regresa aunque actualiza DB

Creo que no entiendes cómo async.series funciona.

Tus funciones en async.series no tome callback como argumento y no lo llaman. Y esa request(...) cosas probablemente no es una función en absoluto. Probablemente por eso rompe el bucle asíncrono. Prueba esto:

async.series(
    [
        function(callback) { // <--- missing callback
            log('starting');
            db.connect('mongodb://127.0.0.1:27017/test',
                function(err, base){
                    if(err) throw err;
                    db = base;
                    callback(); // <--- missing callback
                });
        },
        function(callback) { // <--- missing function with callback
            request(website, function(err,resp,body) {
                start(err, resp, body, callback);
            })
        }
    ],
    function(){
        log('closing DB');
        db.close();
    }
);

Tenga en cuenta que he agregado callback argumento al llamar a start . Por lo tanto, tendrá que refactorizar su código para que cada función acepte callback que se puede llamar al final cuando sabe que todos los trabajos están hechos. Por ejemplo, puede agregar async.parallel dentro de start y esta función puede verse así:

function start(err, resp, body, callback) {
    // some stuff happens here
    var jobs = []
    pageURLS.forEach(function(url, index, array){
        jobs.push(function(clb) {
            request(url, function(error,response,bodies) {
                // some stuff
                clb(); // <--- this refers to the local callback for the job
            });
        });
    });
    async.parallel(jobs, function() {
        // all jobs are done, let's finilize everything
        callback();
    });
};