问题描述
我正在尝试向我的不和谐机器人添加每小时命令。每小时它应该找到所有用户并向他们添加资金。当该命令不在 setinterval 或 settimeout 函数内时,它可以正常工作。但是,该命令需要每小时运行一次,因此 setInterval 命令是最佳选择。这是我的代码。
const economy = require('./economy')
const profileSchema = require('./schemas/profile-schema')
module.exports = async client => {
let J = 0
var interval = setInterval (function () {
profileSchema.find({
}).exec(async (err,res) => {
if (err) {
console.log(err)
}
console.log(res.length)
if (res.length === 0) {
console.log('No users.')
return
} else {
console.log(res.length)
for ( i= 0; i < res.length; i++) {
const userId = res[i].userId
let hourly = await economy.hourlyIncome(userId)
await economy.addCoins(userId,hourly)
if (i < res.length) {
J = 0
}
}
// console.log('Done')
}
console.log('Done')
})
},3000);
}
确切的错误说:
/Users/*******/discordFamilyBot/hourlyIncome.js:14
console.log(res.length)
^
TypeError: Cannot read property 'length' of undefined
我尝试将函数间接放入 setInterval 循环中,但即使这样也会出现相同的错误。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)