NodeJs:Mongodb 从未使用 node-cron 在 cron 作业中调用查询

NodeJs: Mongodb query never called in cron job with node-cron

我已经创建了一个 cron 作业来更新数据库中的一些数据,但是查询从未执行过。

我的 cron 作业:

    const cron = require('node-cron');
    const Document = require('../models/document');
    const User = require('../models/user');
    const checkDocumentsDate = cron.schedule(
      '*/1 * * * *',
      async () => {
        console.log('here');
        Document.find().populate('user', (err, docs) => {
          console.log(err);
          if (!err) {
            docs.forEach((doc) => {
              console.log(doc);
            });
          }
        });
      },
      {
        scheduled: false,
      }
    );
    
    exports.startCheckDocumentsDate = () => {
      checkDocumentsDate.start();
    };

我将 app.js 中的 startCheckDocumentsDate 调用为:

    // CronJobs
    const { startCheckDocumentsDate } = require('./services/cron_tasks');
    // DB Connection
    mongoose
      .connect(process.env.DATABASE, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useCreateIndex: true,
      })
      .then(() => {
        console.log('DB CONNECTED');
        startCheckDocumentsDate();
      });

我的 console.log() :

DB CONNECTED
here  ===> console.log() inside cron job
here
here
here
here
here
here
here

这是我的文档模型,用户模型也是这样的:

    const mongoose = require('mongoose');
    const mongoosePaginate = require('mongoose-paginate-v2');
    
    const { ObjectId } = mongoose.Schema;
    const documentSchema = new mongoose.Schema(
      {
        doctype: { type: String, required: true },
        url: {
          type: String,
        },
        filename: { type: String, required: true },
        fileid: { type: String, required: true, unique: true },
        verificationcount: {
          type: Number,
          default: 0,
        },
        verificationfailedcount: {
          type: Number,
          default: 0,
        },
        expiredate: Date,
        user: {
          type: ObjectId,
          ref: 'User',
          required: true,
        },
        idstabile: {
          type: ObjectId,
          ref: 'Stabile',
        },
        opinionby: [
          {
            type: ObjectId,
            ref: 'User',
          },
        ],
        opinionbyadditionalinfo: [
          {
            user: {
              type: ObjectId,
              ref: 'User',
            },
            approved: {
              type: Boolean,
            },
          },
        ],
      },
    
      { timestamps: true },
    );
    
    documentSchema.index({ user: 1, filename: 1 }, { unique: true });
    documentSchema.plugin(mongoosePaginate);
    
    module.exports = mongoose.model('Document', documentSchema);

找不到它不工作的原因。应该是有问题吧。

另外 moongose 版本是 "mongoose": "^5.7.7"

尝试使用 .then 而不是在 .populate 中定义回调:

Document.find().populate('user').then((err, docs) => {

由于您使用的是 async 函数,您还可以重构代码以使用 await 语法:

const checkDocumentsDate = cron.schedule('*/1 * * * *', async () => {
  try {
    console.log('here');
    let docs = await Document.find().populate('user');
    docs.forEach((doc) => { console.log(doc); });
  } catch(error) {
    console.log(err);
  }
},{
  scheduled: false,
});