0
  • Hello, I have a little problem, I want display every user's email from database Mongo in console.log();.
  • When I start my CRON I have result UNDEFINED.
  • Thanks for your answer

my code

var User = require('./models/user');

var CronJob = require('cron').CronJob;
var job = new CronJob('0-59 0-59 0-23 * * 0-6', function(req, res) {


    User.find(function(err, user) {
        console.log("CRON is started !" + user.email)
    });

}, function () {
    /* This function is executed when the job stops */
    console.log("CRON is stoped !")
},
true /* Start the job right now */
);

1 Answers1

1

I guess your are using Mongoose for the MongoDB related stuff and you defined your own User model. The result from MongoDb will be an array of objects so user.email is not possible. You have to iterate over all returned objects in the array.

Try using:

// get all the users
User.find({}, function(err, users) {

  // optional but i would recommend to use it so that you can see possible errors with your query
  if (err) throw err;

  // iterate over all users of all the users
  users.forEach(function(user) {
    console.log("CRON is started !" + user.email);
  });
});
rieckpil
  • 10,470
  • 3
  • 32
  • 56