CRON NodeJs mongodb [英] CRON NodeJs mongodb

查看:80
本文介绍了CRON NodeJs mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 您好,我有一个小问题,我想在console.log();中显示来自数据库Mongo的每个用户的电子邮件。

  • 启动CRON时,我得到的结果是不确定的。

  • 感谢您的回答


我的代码

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 */
);


推荐答案

我想您正在将Mongoose用于与MongoDB相关的东西您定义了自己的用户模型。 MongoDb的结果将是一个对象数组,因此 user.email 是不可能的。您必须遍历数组中所有返回的对象。

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.

尝试使用:

// 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);
  });
});

这篇关于CRON NodeJs mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆