【发布时间】:2021-08-04 12:01:40
【问题描述】:
我知道这个问题之前已经回答过,但我似乎无法将更改实施到我正在使用的内容中。我正在尝试创建一个日常命令来奖励用户执行 s!daily。我得到了错误,
TypeError: profileData.findOneAndUpdate 不是函数
在 Object.execute (C:\Users--\Desktop\DiscBot\commands\daily.js:35:43)
在 module.exports (C:\Users--\Desktop\DiscBot\events\client\message.js:34:13)
daily.js,第 35 行的 findOneAndUpdate 错误不是函数
const Schema = require('../models/profileSchema')
//cache users that claim daily rewards
let claimedCache = []
const clearCache = () => {
claimedCache = []
setTimeout(clearCache, 1000 * 60 * 10)
}
clearCache()
//message to make it easier later
const alreadyClaimed = 'You have already claimed your daily rewards'
module.exports = {
name: "daily",
aliases: ["day", "d"],
permissions: [],
description: "Claim your daily rewards!",
async execute(message, args, cmd, client, Discord, profileData) {
const { serverID, member } = message
const { id } = member
//If user is in cache return message
if (claimedCache.includes(id)) {
console.log('Returning from cache')
message.reply(alreadyClaimed)
return
}
//Put everything in object for later
const obj = {
guildId: serverID,
userId: id,
}
//Results is an update that either updates if is user is not in array and doesn't if they are, but it doesn't know what findOneAndUpdate is (thought it was just a mongo/mongoose function??)
try {
const results = await profileData.findOneAndUpdate(obj)
console.log('RESULTS:', results)
if (results) {
const then = new Date(results.updatedAt).getTime()
const now = new Date().getTime()
const diffTime = Math.abs(now - then)
const diffDays = Math.round(diffTime / (1000 * 60 * 60 * 24))
if (diffDays <= 1) {
claimedCache.push(id)
message.reply(alreadyClaimed)
return
}
}
//after the update increase coins by 50 and send claimed message
await profileRewardsSchema.findOneAndUpdate(obj, obj, {
upsert: true,
})
claimedCache.push(id)
const amount = 50;
await profileModel.findOneAndUpdate(
{
userID: id,
},
{
$inc: {
coins: amount,
},
}
);
message.reply('You have claimed your daily rewards!')
}catch (err) {
console.log(err);
}
}
}
message.js,这里是我使用 mongoose 将 profileModel 传递到我的命令的地方
const profileModel = require("../../models/profileSchema");
const config = require('../../config.json');
module.exports = async (Discord, client, message) => {
//command handler start
const prefix = 's!';
if (!message.content.startsWith(prefix) || message.author.bot) return;
//database junk
let profileData;
try {
profileData = await profileModel.findOne({ userID: message.author.id });
if (!profileData) {
let profile = await profileModel.create({
userID: message.author.id,
serverID: message.guild.id,
coins: 10,
bank: 0,
});
profile.save();
}
} catch (err) {
console.log("Error creating new database profile");
}
const args = message.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();
const command = client.commands.get(cmd) || client.commands.find(a => a.aliases && a.aliases.includes(cmd));
if(!command) return message.channel.send(":x: This is not a valid command");
try {
command.execute(message, args, cmd, client, Discord, profileData);
} catch (err) {
message.reply('There was an error executing that command!');
}
};
profileSchema.js,其中profile被制作成mongo数据库
const mongoose = require("mongoose");
const profileSchema = new mongoose.Schema({
userID: { type: String, require: true, unique: true },
serverID: { type: String, require: true },
coins: { type: Number, default: 10 },
bank: { type: Number },
},
{
timestamps: true,
}
)
const model = mongoose.model("ProfileModels", profileSchema);
module.exports = model;
main.js,连接mongoose的地方,然后传下去
mongoose.connect(process.env.MONGODB_SRV, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
})
【问题讨论】:
-
在此处添加您的代码,而不是在外部站点上。什么是
profileData。您的“profileSchema”是什么样的?您的架构定义的来源"../../models/profileSchema"? -
@Marc 我编辑了这篇文章,我把它放到了一个 sourcebin 中,因为 SO 不喜欢一篇文章中的大量代码,但是让它工作
-
我通读了 mongoose 文档,了解该功能的工作原理,但不知道该放在哪里。我尝试自己实现它,但对它的工作原理非常迷茫。这就是我在这里的原因......我在哪里/放什么不仅有助于函数的工作原理
-
对于以后的帖子,包括所有相关标签,例如 Mongoose 和 MongoDB
标签: javascript mongodb mongoose discord discord.js