Javascript 节点计划运行任务的次数太多
我为自己构建了一个随机qoute生成器,每天发送一封带有报价的电子邮件。 为了构建它,我使用了节点计划包 我指定它每天16:00执行一个函数:Javascript 节点计划运行任务的次数太多,javascript,node.js,express,node-schedule,Javascript,Node.js,Express,Node Schedule,我为自己构建了一个随机qoute生成器,每天发送一封带有报价的电子邮件。 为了构建它,我使用了节点计划包 我指定它每天16:00执行一个函数: schedule.scheduleJob("* * 16 * * *", () => { scheduler(); console.log("hey"); }); 但事实证明,它这样做了很多次,而不仅仅是一次 有什么问题吗 以下是完整的代码: const express = require("express"); const app =
schedule.scheduleJob("* * 16 * * *", () => {
scheduler();
console.log("hey");
});
但事实证明,它这样做了很多次,而不仅仅是一次
有什么问题吗
以下是完整的代码:
const express = require("express");
const app = express();
const nodemailer = require("nodemailer");
const fetch = require("node-fetch");
const schedule = require("node-schedule");
app.get("*", (req, res) => {
res.send("<h1>Hello, wolrd!</h1>");
});
schedule.scheduleJob("* * 16 * * *", () => {
scheduler();
console.log("hey");
});
function scheduler() {
let qoute = {};
fetch("https://api.quotable.io/random", {
method: "GET"
})
.then(data => {
return data.json();
})
.then(info => {
qoute.qoute = info.content;
qoute.author = info.author;
mailer(qoute);
})
.catch(error => console.error(error));
}
// async..await is not allowed in global scope, must use a wrapper
async function mailer(qoute) {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
// let testAccount = await nodemailer.createTestAccount();
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "mail.google.com",
port:123,
secure: false, // true for 465, false for other ports
auth: {
user: "robot@gmail.com", // generated ethereal user
pass: "123456" // generated ethereal password
}
});
// send mail with defined transport object
let info = await transporter.sendMail(
{
from: '"John doe You need to pass minutes and seconds (optional) as well.
"* * 16 * * *" means it will run at every minute and every second from 16:00 till 17:00.
So you need to correct time like this
schedule.scheduleJob("0 16 * * *", () => {
scheduler();
console.log("hey");
});
const express=require(“express”);
常量app=express();
const nodemailer=require(“nodemailer”);
const fetch=require(“节点获取”);
const schedule=require(“节点计划”);
应用程序获取(“*”,(请求,请求)=>{
res.send(“你好,沃尔德!”);
});
schedule.scheduleJob(“**16***”,()=>{
调度程序();
console.log(“嘿”);
});
函数调度程序(){
设qoute={};
取回(“https://api.quotable.io/random", {
方法:“获取”
})
。然后(数据=>{
返回data.json();
})
。然后(信息=>{
qoute.qoute=信息内容;
qoute.author=info.author;
梅勒(库特);
})
.catch(error=>console.error(error));
}
//全局作用域中不允许async..await,必须使用包装器
异步函数邮件器(qoute){
//从ethereal.email生成测试SMTP服务帐户
//仅当您没有用于测试的真实邮件帐户时才需要
//让testAccount=等待NodeEmailer.createTestAccount();
//使用默认SMTP传输创建可重用的传输对象
让transporter=nodeEmailer.createTransport({
主持人:“mail.google.com”,
港口:123,
安全:false,//对于465为true,对于其他端口为false
认证:{
用户:“robot@gmail.com“,//生成的ethereal用户
通过:“123456”//生成以太密码
}
});
//使用定义的传输对象发送邮件
let info=wait transporter.sendMail(
{
from:“”John doe您还需要通过分钟和秒(可选)。
“**16***”表示从16:00到17:00,每分钟每秒运行一次。
所以你需要像这样修正时间
我不建议在其他函数中定义变量或函数。例如,你说在函数调度程序中定义引号。我建议在函数之外或更高的范围内这样做。