繁体   English   中英

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

我的控制台.log():

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

这是我的文档 model,用户 model 也看起来像:

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

找不到为什么它不起作用。 可能有什么问题。

月牙版本也是 "mongoose": "^5.7.7"

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

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

由于您使用的是async function,因此您还可以重构代码以使用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,
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM