Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

337
Vistas
How to run Mongoose/MongoDB updates/queries in node.js backend without starting the server? (Heroku Scheduler)

I have an app hosted on Heroku (https://trendedge.app) (front-end and back-end are separate apps), and am working on implementing the Heroku Scheduler for basic tasks such as sending emails, and running updates on my MongoDB Atlas database (hit an API, update MongoDB w/ Mongoose).

For the MongoDB updates, I currently hit an admin route on the front-end, and the updates start running on the backend. Easy PZ.

To setup the Heroku scheduler, though, it requests a node.js command (seen here) ...Heroku Scheduler Instructions

... and this makes sense, but if my connection to MongoDB atlas is made upon the starting up the server ('npm start', i.e. 'node index.js'), then how do I run a node command to update MongoDB, when the server hasn't been started yet?

For example, if I run "node updateDB.js", it will error out because there is no server running, and connection to Mongo has yet to be established.

Any insight to how I can setup/test these scheduled node functions (without starting up the server?) would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Can require just the necessary startups from index.js in schedule.js. In this instance just the validation and mongodb startups

about 4 years ago · Juan Pablo Isaza Denunciar

0

for a run single file and connect with database to perform some task. you can try this way.

mongodb = require('mongodb');
config = module.exports = require("/home/node/myclass/crons/config.json");

var MongoClient = mongodb.MongoClient;
ObjectId = module.exports = mongodb.ObjectID;
var dbConnUrl = 'mongodb://' + config.DB_USERNAME + ':' + config.DB_PASSWORD + '@' + config.DB_HOST + ':' + config.DB_PORT + '/' + config.DB_NAME;
console.log("dbConnUrl >> ", dbConnUrl);
MongoClient.connect(dbConnUrl, { useNewUrlParser: true, useUnifiedTopology: true }, function (err, dclient) {
    if (err) {
        console.log("mongodb connection error >> ", err);
        process.exit();
    } else {
        db = module.exports = dclient.db();
        console.log("---------------------------mongodb connected-----------------------");
        run();
    }
});

async function run() {
    await db.collection("notification_token").find({}).toArray();
    process.exit();
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda